** Organize data in a format that is ready for parent-level analysis, for
** parents in the middle generation.  Includes calculation of max EITC
** amount each middle-generation parent could be eligible for, based on 
** that person's number of kids and state of residence.

** Also extract some information that will eventually be merged in with
** grandparent files:
**		The maximum EITC amount for any mid-gen parent in the HH (in a given month)

** NOTE: Use Mod_DepCount in EITC calculations for 1993, but DepCount for
** EITC calculations in later panels.  In 1993, only parent is identified 
** in parent pointer variable, but in subsequent panels there are mom and 
** dad identifiers.  Mod_DepCount does a better job when there is one
** parent identifier, DepCount does a better job when both parents have 
** identifiers.



*******************************************************************************



*******************
** 1990 PANEL
*******************

***********
** Wave 3
***********


use  MidGenParentData1990_w3
** Merge in count of how many people in household identified this person as his/her mother
merge 1:1 suid addid pnum srefmon using KidCount1990_w3
assert _merge~=1
keep if _merge==3
drop _merge


** EXPERIMENTING: Try a version where you DON'T subtract one from year
** before doing EITC calculations
gen EITC_calc_year = (1900 + rhcalyr)
gen xocah = Mod_DepCount
quietly do EITC_Calculator.do
drop MaxFedEITC
drop MaxStateEITC
drop StateEITC_NonRefund
rename r_MaxEITC currentyear_r_MaxEITC
rename r_MaxEITC_thou currentyear_r_MaxEITC_thou
rename r_MaxFedEITC_thou currentyear_r_MaxFedEITC_thou
rename r_MaxStateEITC_thou  currentyear_r_MaxStateEITC_thou
rename r_ref_MaxEITC  currentyear_r_ref_MaxEITC
drop real
drop EITC_calc_year xocah


** Now back to original way of calculating EITC amounts
gen EITC_calc_year = (1900+rhcalyr)-1
gen xocah = Mod_DepCount
quietly do EITC_Calculator.do



**********************************************************
** Now merge in simulated EITC amount (average for cell defined
** in terms of YearOfTaxCalc, tfipsst, Married, KidsForSimulated
gen YearOfTaxCalc = (1900+rhcalyr)-1
gen KidsForSimulated = Mod_DepCount
replace KidsForSimulated = 3 if KidsForSimulated>3
merge m:1 YearOfTaxCalc tfipsst Married KidsForSimulated using SimulatedEITC
drop if _merge==2
drop _merge
gen taxsim_eic_thou = (ts_eic_fed + ts_eic_state)/1000
*******************************************************************



rename Married MidGenPar_Married
rename Employed MidGenPar_Employed
rename HSGrad MidGenPar_HSGrad
rename SomeCollege MidGenPar_SomeCollege
rename CollegeGrad MidGenPar_CollegeGrad
rename White MidGenPar_White
rename PotentialDep MidGenPar_PotentialDep

gen SingleMom = (Female==1 & MidGenPar_Married==0)
gen SingleNoCollegeMom = (Female==1 & MidGenPar_Married==0 & MidGenPar_SomeCollege==0 & MidGenPar_CollegeGrad==0)
gen temp_college = (MidGenPar_SomeCollege==1 | MidGenPar_CollegeGrad==1)


sort suid addid srefmon
by suid addid srefmon: egen N_SingleMom=total(SingleMom)
by suid addid srefmon: egen N_SingleNoCollegeMom = total(SingleNoCollegeMom)
by suid addid srefmon: egen N_ParsWithCollege=total(temp_college)
by suid addid srefmon: egen N_CollegeGradPars=total(MidGenPar_CollegeGrad)
by suid addid srefmon: egen N_ParsEmployed=total(MidGenPar_Employed)
by suid addid srefmon: egen N_ParsPotentialDep=total(MidGenPar_PotentialDep)


gen AnyParsPotentialDeps=(N_ParsPotentialDep>0)
gen AnySingleMom = (N_SingleMom>0)
gen AnySingleNoCollegeMom=(N_SingleNoCollegeMom>0)
gen NoParsWithCollege = (N_ParsWithCollege==0)
gen NoCollegeGradPars = (N_CollegeGradPars==0)
gen AnyParsEmployed = (N_ParsEmployed>0)

drop temp_college


gen RefPerson=(errp==1 | errp==2)
sort suid addid srefmon
by suid addid srefmon: egen N_RefPerson=total(RefPerson)
gen MidGenHasRefPerson=(N_RefPerson>0)


tostring suid, replace
merge m:1 suid entry pnum using WellBeingVars1990_w3 


keep if _merge==3
drop _merge




*******************************************************************************
** Merge in data on receipt of childcare from a grandparent 

merge m:1 suid entry pnum using ChildCareVars_1990_w3
drop if _merge==2
drop _merge 

sort suid addid srefmon
by suid addid srefmon: egen N_GetCCFromGP=total(GetCCFromGP)
gen AnyParsGetCCFromGP = (N_GetCCFromGP>0)


gen panel=1990
save OrganizedMidGenParentData1990_w3, replace


** Extract information at household-month level about MAXIMUM amount of EITC eligibility
** for which any mid-gen parent is available.  The goal is to make this info ready 
** for merging with grandparents who live in those same households.
** Also extract info about whether the middle generation includes any single mom,
** or any single mom with no college education, any employed parents, a HH ref person, etc
** SPRING 2022 UPDATE: Do for three measures of EITC: legislated max EITC using lagged tax 
** year, legislated max EITC using current tax year, simulated EITC 


keep panel swave suid addid srefmon r_MaxEITC_thou r_ref_MaxEITC currentyear_r_MaxEITC_thou currentyear_r_ref_MaxEITC taxsim_eic_thou xocah AnySingleMom AnySingleNoCollegeMom NoParsWithCollege NoCollegeGradPars AnyParsEmployed MidGenHasRefPerson  AnyParsPotentialDeps r_MaxFedEITC_thou ts_eic_fed AnyParsGetCCFromGP
sort suid addid srefmon
by suid addid srefmon: egen r_MaxEITC_MidGenMax=max(r_MaxEITC_thou)
by suid addid srefmon: egen r_ref_MaxEITC_MidGenMax=max(r_ref_MaxEITC)
by suid addid srefmon: egen currentyear_r_MaxEITC_MidGenMax=max(currentyear_r_MaxEITC_thou)
by suid addid srefmon: egen taxsim_eic_MidGenMax=max(taxsim_eic_thou)
by suid addid srefmon: egen r_MaxFedEITC_MidGenMax = max(r_MaxFedEITC_thou)
by suid addid srefmon: egen taxsim_eic_Fed_MidGenMax = max(ts_eic_fed)



** Only keep observations of individuals whose EITC eligibility is equal to the max EITC
** eligibility of anyone in the middle generation  (just a way of keeping one record per HH)
keep if r_MaxEITC_MidGenMax == r_MaxEITC_thou




** For later construction of a summary stats table, distinguish between cases of 1 EITC-eligible
** child and 2+ EITC-eligible children of the middle-generation parent who is eligible for the largest
** EITC amount
gen xocah_1or2plus=.
replace xocah_1or2plus = 1 if xocah==1
replace xocah_1or2plus = 2 if xocah>1 

drop xocah* r_MaxEITC_thou r_ref_MaxEITC currentyear_r_ref_MaxEITC currentyear_r_MaxEITC_thou taxsim_eic_thou ts_eic_fed
duplicates drop

duplicates report suid addid srefmon


save MaxMidGenEITC1990_w3, replace
clear



** Another way to count EITC resources in the middle generation is to sum up amounts received
** by different mid-gen parents.  You don't want to double-count EITC eligibility of a husband
** and wife, but you could assume that all mothers are separate filers and sum up all EITC
** amounts of the different mid-gen mothers.  
use OrganizedMidGenParentData1990_w3

keep panel swave suid addid srefmon r_MaxEITC_thou r_ref_MaxEITC currentyear_r_MaxEITC_thou currentyear_r_ref_MaxEITC taxsim_eic_thou PotentialMidGenMom r_MaxFedEITC_thou ts_eic_fed
keep if PotentialMidGenMom==1
duplicates report suid addid srefmon
sort suid addid srefmon
by suid addid srefmon: egen Sum_MaxEITC_MidGenMoms = sum(r_MaxEITC_thou)
by suid addid srefmon: egen Sum_ref_MaxEITC_MidGenMoms = sum(r_ref_MaxEITC)
by suid addid srefmon: egen Sum_cy_MaxEITC_MidGenMoms = sum(currentyear_r_MaxEITC_thou)
by suid addid srefmon: egen Sum_cy_ref_MaxEITC_MidGenMoms = sum(currentyear_r_ref_MaxEITC)
by suid addid srefmon: egen Sum_taxsim_eic_MidGenMoms = sum(taxsim_eic_thou)
by suid addid srefmon: egen Sum_MaxFedEITC_MidGenMoms = sum(r_MaxFedEITC_thou)
by suid addid srefmon: egen Sum_taxsim_eic_Fed_MidGenMoms = sum(ts_eic_fed)

keep panel swave suid addid  srefmon Sum_MaxEITC_MidGenMoms Sum_ref_MaxEITC_MidGenMoms Sum_cy_MaxEITC_MidGenMoms Sum_cy_ref_MaxEITC_MidGenMoms Sum_taxsim_eic_MidGenMoms Sum_MaxFedEITC_MidGenMoms Sum_taxsim_eic_Fed_MidGenMoms
duplicates drop
save SumMidGenEITC_1990_w3, replace
clear



***********
** Wave 4
***********


use  MidGenParentData1990_w4
** Merge in count of how many people in household identified this person as his/her mother
merge 1:1 suid addid pnum srefmon using KidCount1990_w4
assert _merge~=1
keep if _merge==3
drop _merge


** EXPERIMENTING: Try a version where you DON'T subtract one from year
** before doing EITC calculations
gen EITC_calc_year = (1900 + rhcalyr)
gen xocah = Mod_DepCount
quietly do EITC_Calculator.do
drop MaxFedEITC
drop MaxStateEITC
drop StateEITC_NonRefund
rename r_MaxEITC currentyear_r_MaxEITC
rename r_MaxEITC_thou currentyear_r_MaxEITC_thou
rename r_MaxFedEITC_thou currentyear_r_MaxFedEITC_thou
rename r_MaxStateEITC_thou  currentyear_r_MaxStateEITC_thou
rename r_ref_MaxEITC  currentyear_r_ref_MaxEITC
drop real
drop EITC_calc_year xocah


** Now back to original way of calculating EITC amounts
gen EITC_calc_year = (1900+rhcalyr)-1
gen xocah = Mod_DepCount
quietly do EITC_Calculator.do



**********************************************************
** Now merge in simulated EITC amount (average for cell defined
** in terms of YearOfTaxCalc, tfipsst, Married, KidsForSimulated
gen YearOfTaxCalc = (1900+rhcalyr)-1
gen KidsForSimulated = Mod_DepCount
replace KidsForSimulated = 3 if KidsForSimulated>3
merge m:1 YearOfTaxCalc tfipsst Married KidsForSimulated using SimulatedEITC
drop if _merge==2
drop _merge
gen taxsim_eic_thou = (ts_eic_fed + ts_eic_state)/1000
*******************************************************************



rename Married MidGenPar_Married
rename Employed MidGenPar_Employed
rename HSGrad MidGenPar_HSGrad
rename SomeCollege MidGenPar_SomeCollege
rename CollegeGrad MidGenPar_CollegeGrad
rename White MidGenPar_White
rename PotentialDep MidGenPar_PotentialDep

gen SingleMom = (Female==1 & MidGenPar_Married==0)
gen SingleNoCollegeMom = (Female==1 & MidGenPar_Married==0 & MidGenPar_SomeCollege==0 & MidGenPar_CollegeGrad==0)
gen temp_college = (MidGenPar_SomeCollege==1 | MidGenPar_CollegeGrad==1)


sort suid addid srefmon
by suid addid srefmon: egen N_SingleMom=total(SingleMom)
by suid addid srefmon: egen N_SingleNoCollegeMom = total(SingleNoCollegeMom)
by suid addid srefmon: egen N_ParsWithCollege=total(temp_college)
by suid addid srefmon: egen N_CollegeGradPars=total(MidGenPar_CollegeGrad)
by suid addid srefmon: egen N_ParsEmployed=total(MidGenPar_Employed)
by suid addid srefmon: egen N_ParsPotentialDep=total(MidGenPar_PotentialDep)


gen AnyParsPotentialDeps=(N_ParsPotentialDep>0)
gen AnySingleMom = (N_SingleMom>0)
gen AnySingleNoCollegeMom=(N_SingleNoCollegeMom>0)
gen NoParsWithCollege = (N_ParsWithCollege==0)
gen NoCollegeGradPars = (N_CollegeGradPars==0)
gen AnyParsEmployed = (N_ParsEmployed>0)

drop temp_college


gen RefPerson=(errp==1 | errp==2)
sort suid addid srefmon
by suid addid srefmon: egen N_RefPerson=total(RefPerson)
gen MidGenHasRefPerson=(N_RefPerson>0)



tostring suid, replace
merge m:1 suid entry pnum using WellBeingVars1990_w4 


keep if _merge==3
drop _merge





gen panel=1990
save OrganizedMidGenParentData1990_w4, replace


** Extract information at household-month level about MAXIMUM amount of EITC eligibility
** for which any mid-gen parent is available.  The goal is to make this info ready 
** for merging with grandparents who live in those same households.
** Also extract info about whether the middle generation includes any single mom,
** or any single mom with no college education, any employed parents, a HH ref person, etc
** SPRING 2022 UPDATE: Do for three measures of EITC: legislated max EITC using lagged tax 
** year, legislated max EITC using current tax year, simulated EITC 


keep panel swave suid addid srefmon r_MaxEITC_thou r_ref_MaxEITC currentyear_r_MaxEITC_thou currentyear_r_ref_MaxEITC taxsim_eic_thou xocah AnySingleMom AnySingleNoCollegeMom NoParsWithCollege NoCollegeGradPars AnyParsEmployed MidGenHasRefPerson  AnyParsPotentialDeps r_MaxFedEITC_thou ts_eic_fed

sort suid addid srefmon
by suid addid srefmon: egen r_MaxEITC_MidGenMax=max(r_MaxEITC_thou)
by suid addid srefmon: egen r_ref_MaxEITC_MidGenMax=max(r_ref_MaxEITC)
by suid addid srefmon: egen currentyear_r_MaxEITC_MidGenMax=max(currentyear_r_MaxEITC_thou)
by suid addid srefmon: egen taxsim_eic_MidGenMax=max(taxsim_eic_thou)
by suid addid srefmon: egen r_MaxFedEITC_MidGenMax = max(r_MaxFedEITC_thou)
by suid addid srefmon: egen taxsim_eic_Fed_MidGenMax = max(ts_eic_fed)



** Only keep observations of individuals whose EITC eligibility is equal to the max EITC
** eligibility of anyone in the middle generation  (just a way of keeping one record per HH)
keep if r_MaxEITC_MidGenMax == r_MaxEITC_thou


** For later construction of a summary stats table, distinguish between cases of 1 EITC-eligible
** child and 2+ EITC-eligible children of the middle-generation parent who is eligible for the largest
** EITC amount
gen xocah_1or2plus=.
replace xocah_1or2plus = 1 if xocah==1
replace xocah_1or2plus = 2 if xocah>1 

drop xocah* r_MaxEITC_thou r_ref_MaxEITC currentyear_r_ref_MaxEITC currentyear_r_MaxEITC_thou taxsim_eic_thou ts_eic_fed
duplicates drop

duplicates report suid addid srefmon


save MaxMidGenEITC1990_w4, replace
clear



** Another way to count EITC resources in the middle generation is to sum up amounts received
** by different mid-gen parents.  You don't want to double-count EITC eligibility of a husband
** and wife, but you could assume that all mothers are separate filers and sum up all EITC
** amounts of the different mid-gen mothers.  
use OrganizedMidGenParentData1990_w4

keep panel swave suid addid srefmon r_MaxEITC_thou r_ref_MaxEITC currentyear_r_MaxEITC_thou currentyear_r_ref_MaxEITC taxsim_eic_thou PotentialMidGenMom r_MaxFedEITC_thou ts_eic_fed
keep if PotentialMidGenMom==1
duplicates report suid addid srefmon
sort suid addid srefmon
by suid addid srefmon: egen Sum_MaxEITC_MidGenMoms = sum(r_MaxEITC_thou)
by suid addid srefmon: egen Sum_ref_MaxEITC_MidGenMoms = sum(r_ref_MaxEITC)
by suid addid srefmon: egen Sum_cy_MaxEITC_MidGenMoms = sum(currentyear_r_MaxEITC_thou)
by suid addid srefmon: egen Sum_cy_ref_MaxEITC_MidGenMoms = sum(currentyear_r_ref_MaxEITC)
by suid addid srefmon: egen Sum_taxsim_eic_MidGenMoms = sum(taxsim_eic_thou)
by suid addid srefmon: egen Sum_MaxFedEITC_MidGenMoms = sum(r_MaxFedEITC_thou)
by suid addid srefmon: egen Sum_taxsim_eic_Fed_MidGenMoms = sum(ts_eic_fed)


keep panel swave suid addid  srefmon Sum_MaxEITC_MidGenMoms Sum_ref_MaxEITC_MidGenMoms Sum_cy_MaxEITC_MidGenMoms Sum_cy_ref_MaxEITC_MidGenMoms Sum_taxsim_eic_MidGenMoms Sum_MaxFedEITC_MidGenMoms Sum_taxsim_eic_Fed_MidGenMoms
duplicates drop
save SumMidGenEITC_1990_w4, replace
clear





*******************
** 1991
*******************

***********
** Wave 3
***********


use  MidGenParentData1991_w3
** Merge in count of how many people in household identified this person as his/her mother
merge 1:1 suid addid pnum srefmon using KidCount1991_w3
assert _merge~=1
keep if _merge==3
drop _merge


** EXPERIMENTING: Try a version where you DON'T subtract one from year
** before doing EITC calculations
gen EITC_calc_year = (1900 + rhcalyr)
gen xocah = Mod_DepCount
quietly do EITC_Calculator.do
drop MaxFedEITC
drop MaxStateEITC
drop StateEITC_NonRefund
rename r_MaxEITC currentyear_r_MaxEITC
rename r_MaxEITC_thou currentyear_r_MaxEITC_thou
rename r_MaxFedEITC_thou currentyear_r_MaxFedEITC_thou
rename r_MaxStateEITC_thou  currentyear_r_MaxStateEITC_thou
rename r_ref_MaxEITC  currentyear_r_ref_MaxEITC
drop real
drop EITC_calc_year xocah


** Now back to original way of calculating EITC amounts
gen EITC_calc_year = (1900+rhcalyr)-1
gen xocah = Mod_DepCount
quietly do EITC_Calculator.do



**********************************************************
** Now merge in simulated EITC amount (average for cell defined
** in terms of YearOfTaxCalc, tfipsst, Married, KidsForSimulated
gen YearOfTaxCalc = (1900+rhcalyr)-1
gen KidsForSimulated = Mod_DepCount
replace KidsForSimulated = 3 if KidsForSimulated>3
merge m:1 YearOfTaxCalc tfipsst Married KidsForSimulated using SimulatedEITC
drop if _merge==2
drop _merge
gen taxsim_eic_thou = (ts_eic_fed + ts_eic_state)/1000
*******************************************************************



rename Married MidGenPar_Married
rename Employed MidGenPar_Employed
rename HSGrad MidGenPar_HSGrad
rename SomeCollege MidGenPar_SomeCollege
rename CollegeGrad MidGenPar_CollegeGrad
rename White MidGenPar_White
rename PotentialDep MidGenPar_PotentialDep

gen SingleMom = (Female==1 & MidGenPar_Married==0)
gen SingleNoCollegeMom = (Female==1 & MidGenPar_Married==0 & MidGenPar_SomeCollege==0 & MidGenPar_CollegeGrad==0)
gen temp_college = (MidGenPar_SomeCollege==1 | MidGenPar_CollegeGrad==1)


sort suid addid srefmon
by suid addid srefmon: egen N_SingleMom=total(SingleMom)
by suid addid srefmon: egen N_SingleNoCollegeMom = total(SingleNoCollegeMom)
by suid addid srefmon: egen N_ParsWithCollege=total(temp_college)
by suid addid srefmon: egen N_CollegeGradPars=total(MidGenPar_CollegeGrad)
by suid addid srefmon: egen N_ParsEmployed=total(MidGenPar_Employed)
by suid addid srefmon: egen N_ParsPotentialDep=total(MidGenPar_PotentialDep)


gen AnyParsPotentialDeps=(N_ParsPotentialDep>0)
gen AnySingleMom = (N_SingleMom>0)
gen AnySingleNoCollegeMom=(N_SingleNoCollegeMom>0)
gen NoParsWithCollege = (N_ParsWithCollege==0)
gen NoCollegeGradPars = (N_CollegeGradPars==0)
gen AnyParsEmployed = (N_ParsEmployed>0)

drop temp_college


gen RefPerson=(errp==1 | errp==2)
sort suid addid srefmon
by suid addid srefmon: egen N_RefPerson=total(RefPerson)
gen MidGenHasRefPerson=(N_RefPerson>0)



tostring suid, replace
merge m:1 suid entry pnum using WellBeingVars1991_w3 


keep if _merge==3
drop _merge





*******************************************************************************
** Merge in data on receipt of childcare from a grandparent 

merge m:1 suid entry pnum using ChildCareVars_1991_w3
drop if _merge==2
drop _merge 

sort suid addid srefmon
by suid addid srefmon: egen N_GetCCFromGP=total(GetCCFromGP)
gen AnyParsGetCCFromGP = (N_GetCCFromGP>0)



gen panel=1991
save OrganizedMidGenParentData1991_w3, replace


** Extract information at household-month level about MAXIMUM amount of EITC eligibility
** for which any mid-gen parent is available.  The goal is to make this info ready 
** for merging with grandparents who live in those same households.
** Also extract info about whether the middle generation includes any single mom,
** or any single mom with no college education, any employed parents, a HH ref person, etc
** SPRING 2022 UPDATE: Do for three measures of EITC: legislated max EITC using lagged tax 
** year, legislated max EITC using current tax year, simulated EITC 


keep panel swave suid addid srefmon r_MaxEITC_thou r_ref_MaxEITC currentyear_r_MaxEITC_thou currentyear_r_ref_MaxEITC taxsim_eic_thou xocah AnySingleMom AnySingleNoCollegeMom NoParsWithCollege NoCollegeGradPars AnyParsEmployed MidGenHasRefPerson  AnyParsPotentialDeps r_MaxFedEITC_thou ts_eic_fed AnyParsGetCCFromGP
sort suid addid srefmon
by suid addid srefmon: egen r_MaxEITC_MidGenMax=max(r_MaxEITC_thou)
by suid addid srefmon: egen r_ref_MaxEITC_MidGenMax=max(r_ref_MaxEITC)
by suid addid srefmon: egen currentyear_r_MaxEITC_MidGenMax=max(currentyear_r_MaxEITC_thou)
by suid addid srefmon: egen taxsim_eic_MidGenMax=max(taxsim_eic_thou)
by suid addid srefmon: egen r_MaxFedEITC_MidGenMax=max(r_MaxFedEITC_thou)
by suid addid srefmon: egen taxsim_eic_Fed_MidGenMax=max(ts_eic_fed)

** Only keep observations of individuals whose EITC eligibility is equal to the max EITC
** eligibility of anyone in the middle generation  (just a way of keeping one record per HH)
keep if r_MaxEITC_MidGenMax == r_MaxEITC_thou


** For later construction of a summary stats table, distinguish between cases of 1 EITC-eligible
** child and 2+ EITC-eligible children of the middle-generation parent who is eligible for the largest
** EITC amount
gen xocah_1or2plus=.
replace xocah_1or2plus = 1 if xocah==1
replace xocah_1or2plus = 2 if xocah>1 

drop xocah* r_MaxEITC_thou r_ref_MaxEITC currentyear_r_ref_MaxEITC currentyear_r_MaxEITC_thou taxsim_eic_thou ts_eic_fed
duplicates drop

duplicates report suid addid srefmon


save MaxMidGenEITC1991_w3, replace
clear



** Another way to count EITC resources in the middle generation is to sum up amounts received
** by different mid-gen parents.  You don't want to double-count EITC eligibility of a husband
** and wife, but you could assume that all mothers are separate filers and sum up all EITC
** amounts of the different mid-gen mothers.  
use OrganizedMidGenParentData1991_w3

keep panel swave suid addid srefmon r_MaxEITC_thou r_ref_MaxEITC currentyear_r_MaxEITC_thou currentyear_r_ref_MaxEITC taxsim_eic_thou PotentialMidGenMom r_MaxFedEITC_thou ts_eic_fed
keep if PotentialMidGenMom==1
duplicates report suid addid srefmon
sort suid addid srefmon
by suid addid srefmon: egen Sum_MaxEITC_MidGenMoms = sum(r_MaxEITC_thou)
by suid addid srefmon: egen Sum_ref_MaxEITC_MidGenMoms = sum(r_ref_MaxEITC)
by suid addid srefmon: egen Sum_cy_MaxEITC_MidGenMoms = sum(currentyear_r_MaxEITC_thou)
by suid addid srefmon: egen Sum_cy_ref_MaxEITC_MidGenMoms = sum(currentyear_r_ref_MaxEITC)
by suid addid srefmon: egen Sum_taxsim_eic_MidGenMoms = sum(taxsim_eic_thou)
by suid addid srefmon: egen Sum_MaxFedEITC_MidGenMoms = sum(r_MaxFedEITC_thou)
by suid addid srefmon: egen Sum_taxsim_eic_Fed_MidGenMoms = sum(ts_eic_fed)

keep panel swave suid addid  srefmon Sum_MaxEITC_MidGenMoms Sum_ref_MaxEITC_MidGenMoms Sum_cy_MaxEITC_MidGenMoms Sum_cy_ref_MaxEITC_MidGenMoms Sum_taxsim_eic_MidGenMoms Sum_MaxFedEITC_MidGenMoms Sum_taxsim_eic_Fed_MidGenMoms
duplicates drop
save SumMidGenEITC_1991_w3, replace
clear





*******************
** 1991
*******************

***********
** Wave 7
***********


use  MidGenParentData1991_w7
** Merge in count of how many people in household identified this person as his/her mother
merge 1:1 suid addid pnum srefmon using KidCount1991_w7
assert _merge~=1
keep if _merge==3
drop _merge


** EXPERIMENTING: Try a version where you DON'T subtract one from year
** before doing EITC calculations
gen EITC_calc_year = (1900 + rhcalyr)
gen xocah = Mod_DepCount
quietly do EITC_Calculator.do
drop MaxFedEITC
drop MaxStateEITC
drop StateEITC_NonRefund
rename r_MaxEITC currentyear_r_MaxEITC
rename r_MaxEITC_thou currentyear_r_MaxEITC_thou
rename r_MaxFedEITC_thou currentyear_r_MaxFedEITC_thou
rename r_MaxStateEITC_thou  currentyear_r_MaxStateEITC_thou
rename r_ref_MaxEITC  currentyear_r_ref_MaxEITC
drop real
drop EITC_calc_year xocah


** Now back to original way of calculating EITC amounts
gen EITC_calc_year = (1900+rhcalyr)-1
gen xocah = Mod_DepCount
quietly do EITC_Calculator.do



**********************************************************
** Now merge in simulated EITC amount (average for cell defined
** in terms of YearOfTaxCalc, tfipsst, Married, KidsForSimulated
gen YearOfTaxCalc = (1900+rhcalyr)-1
gen KidsForSimulated = Mod_DepCount
replace KidsForSimulated = 3 if KidsForSimulated>3
merge m:1 YearOfTaxCalc tfipsst Married KidsForSimulated using SimulatedEITC
drop if _merge==2
drop _merge
gen taxsim_eic_thou = (ts_eic_fed + ts_eic_state)/1000
*******************************************************************



rename Married MidGenPar_Married
rename Employed MidGenPar_Employed
rename HSGrad MidGenPar_HSGrad
rename SomeCollege MidGenPar_SomeCollege
rename CollegeGrad MidGenPar_CollegeGrad
rename White MidGenPar_White
rename PotentialDep MidGenPar_PotentialDep

gen SingleMom = (Female==1 & MidGenPar_Married==0)
gen SingleNoCollegeMom = (Female==1 & MidGenPar_Married==0 & MidGenPar_SomeCollege==0 & MidGenPar_CollegeGrad==0)
gen temp_college = (MidGenPar_SomeCollege==1 | MidGenPar_CollegeGrad==1)


sort suid addid srefmon
by suid addid srefmon: egen N_SingleMom=total(SingleMom)
by suid addid srefmon: egen N_SingleNoCollegeMom = total(SingleNoCollegeMom)
by suid addid srefmon: egen N_ParsWithCollege=total(temp_college)
by suid addid srefmon: egen N_CollegeGradPars=total(MidGenPar_CollegeGrad)
by suid addid srefmon: egen N_ParsEmployed=total(MidGenPar_Employed)
by suid addid srefmon: egen N_ParsPotentialDep=total(MidGenPar_PotentialDep)


gen AnyParsPotentialDeps=(N_ParsPotentialDep>0)
gen AnySingleMom = (N_SingleMom>0)
gen AnySingleNoCollegeMom=(N_SingleNoCollegeMom>0)
gen NoParsWithCollege = (N_ParsWithCollege==0)
gen NoCollegeGradPars = (N_CollegeGradPars==0)
gen AnyParsEmployed = (N_ParsEmployed>0)

drop temp_college


gen RefPerson=(errp==1 | errp==2)
sort suid addid srefmon
by suid addid srefmon: egen N_RefPerson=total(RefPerson)
gen MidGenHasRefPerson=(N_RefPerson>0)



tostring suid, replace
merge m:1 suid entry pnum using WellBeingVars1991_w7 

** The _merge==2 cases are fine (well-being variables are collected for lots of people
** who aren't parents).  But what about the _merge==1 cases?  These are parents who don't 
** have an entry in well-being variable dataset. Can this happen for legitimate reason?
keep if _merge==3
drop _merge




gen panel=1991
save OrganizedMidGenParentData1991_w7, replace


** Extract information at household-month level about MAXIMUM amount of EITC eligibility
** for which any mid-gen parent is available.  The goal is to make this info ready 
** for merging with grandparents who live in those same households.
** Also extract info about whether the middle generation includes any single mom,
** or any single mom with no college education, any employed parents, a HH ref person, etc
** SPRING 2022 UPDATE: Do for three measures of EITC: legislated max EITC using lagged tax 
** year, legislated max EITC using current tax year, simulated EITC 


keep panel swave suid addid srefmon r_MaxEITC_thou r_ref_MaxEITC currentyear_r_MaxEITC_thou currentyear_r_ref_MaxEITC taxsim_eic_thou xocah AnySingleMom AnySingleNoCollegeMom NoParsWithCollege NoCollegeGradPars AnyParsEmployed MidGenHasRefPerson  AnyParsPotentialDeps r_MaxFedEITC_thou ts_eic_fed
sort suid addid srefmon
by suid addid srefmon: egen r_MaxEITC_MidGenMax=max(r_MaxEITC_thou)
by suid addid srefmon: egen r_ref_MaxEITC_MidGenMax=max(r_ref_MaxEITC)
by suid addid srefmon: egen currentyear_r_MaxEITC_MidGenMax=max(currentyear_r_MaxEITC_thou)
by suid addid srefmon: egen taxsim_eic_MidGenMax=max(taxsim_eic_thou)
by suid addid srefmon: egen r_MaxFedEITC_MidGenMax=max(r_MaxFedEITC_thou)
by suid addid srefmon: egen taxsim_eic_Fed_MidGenMax=max(ts_eic_fed)

** Only keep observations of individuals whose EITC eligibility is equal to the max EITC
** eligibility of anyone in the middle generation  (just a way of keeping one record per HH)
keep if r_MaxEITC_MidGenMax == r_MaxEITC_thou

** For later construction of a summary stats table, distinguish between cases of 1 EITC-eligible
** child and 2+ EITC-eligible children of the middle-generation parent who is eligible for the largest
** EITC amount
gen xocah_1or2plus=.
replace xocah_1or2plus = 1 if xocah==1
replace xocah_1or2plus = 2 if xocah>1 

drop xocah r_MaxEITC_thou r_ref_MaxEITC currentyear_r_ref_MaxEITC currentyear_r_MaxEITC_thou taxsim_eic_thou ts_eic_fed
duplicates drop

duplicates report suid addid srefmon

save MaxMidGenEITC1991_w7, replace
clear



** Another way to count EITC resources in the middle generation is to sum up amounts received
** by different mid-gen parents.  You don't want to double-count EITC eligibility of a husband
** and wife, but you could assume that all mothers are separate filers and sum up all EITC
** amounts of the different mid-gen mothers.  
use OrganizedMidGenParentData1991_w7

keep panel swave suid addid srefmon r_MaxEITC_thou r_ref_MaxEITC currentyear_r_MaxEITC_thou currentyear_r_ref_MaxEITC taxsim_eic_thou PotentialMidGenMom r_MaxFedEITC_thou ts_eic_fed
keep if PotentialMidGenMom==1
duplicates report suid addid srefmon
sort suid addid srefmon
by suid addid srefmon: egen Sum_MaxEITC_MidGenMoms = sum(r_MaxEITC_thou)
by suid addid srefmon: egen Sum_ref_MaxEITC_MidGenMoms = sum(r_ref_MaxEITC)
by suid addid srefmon: egen Sum_cy_MaxEITC_MidGenMoms = sum(currentyear_r_MaxEITC_thou)
by suid addid srefmon: egen Sum_cy_ref_MaxEITC_MidGenMoms = sum(currentyear_r_ref_MaxEITC)
by suid addid srefmon: egen Sum_taxsim_eic_MidGenMoms = sum(taxsim_eic_thou)
by suid addid srefmon: egen Sum_MaxFedEITC_MidGenMoms = sum(r_MaxFedEITC_thou)
by suid addid srefmon: egen Sum_taxsim_eic_Fed_MidGenMoms = sum(ts_eic_fed)

keep panel swave suid addid srefmon Sum_MaxEITC_MidGenMoms Sum_ref_MaxEITC_MidGenMoms Sum_cy_MaxEITC_MidGenMoms Sum_cy_ref_MaxEITC_MidGenMoms Sum_taxsim_eic_MidGenMoms Sum_MaxFedEITC_MidGenMoms Sum_taxsim_eic_Fed_MidGenMoms
duplicates drop
save SumMidGenEITC_1991_w7, replace
clear




*** Some notes about 1992: Financial variables are in wave 4, health in wave 6




*******************
** 1992
*******************

***********
** Wave 4
***********


use  MidGenParentData1992_w4
** Merge in count of how many people in household identified this person as his/her mother
merge 1:1 suid addid pnum srefmon using KidCount1992_w4
assert _merge~=1
keep if _merge==3
drop _merge


** EXPERIMENTING: Try a version where you DON'T subtract one from year
** before doing EITC calculations
gen EITC_calc_year = (1900 + rhcalyr)
gen xocah = Mod_DepCount
quietly do EITC_Calculator.do
drop MaxFedEITC
drop MaxStateEITC
drop StateEITC_NonRefund
rename r_MaxEITC currentyear_r_MaxEITC
rename r_MaxEITC_thou currentyear_r_MaxEITC_thou
rename r_MaxFedEITC_thou currentyear_r_MaxFedEITC_thou
rename r_MaxStateEITC_thou  currentyear_r_MaxStateEITC_thou
rename r_ref_MaxEITC  currentyear_r_ref_MaxEITC
drop real
drop EITC_calc_year xocah


** Now back to original way of calculating EITC amounts
gen EITC_calc_year = (1900+rhcalyr)-1
gen xocah = Mod_DepCount
quietly do EITC_Calculator.do



**********************************************************
** Now merge in simulated EITC amount (average for cell defined
** in terms of YearOfTaxCalc, tfipsst, Married, KidsForSimulated
gen YearOfTaxCalc = (1900+rhcalyr)-1
gen KidsForSimulated = Mod_DepCount
replace KidsForSimulated = 3 if KidsForSimulated>3
merge m:1 YearOfTaxCalc tfipsst Married KidsForSimulated using SimulatedEITC
drop if _merge==2
drop _merge
gen taxsim_eic_thou = (ts_eic_fed + ts_eic_state)/1000
*******************************************************************



rename Married MidGenPar_Married
rename Employed MidGenPar_Employed
rename HSGrad MidGenPar_HSGrad
rename SomeCollege MidGenPar_SomeCollege
rename CollegeGrad MidGenPar_CollegeGrad
rename White MidGenPar_White
rename PotentialDep MidGenPar_PotentialDep

gen SingleMom = (Female==1 & MidGenPar_Married==0)
gen SingleNoCollegeMom = (Female==1 & MidGenPar_Married==0 & MidGenPar_SomeCollege==0 & MidGenPar_CollegeGrad==0)
gen temp_college = (MidGenPar_SomeCollege==1 | MidGenPar_CollegeGrad==1)


sort suid addid srefmon
by suid addid srefmon: egen N_SingleMom=total(SingleMom)
by suid addid srefmon: egen N_SingleNoCollegeMom = total(SingleNoCollegeMom)
by suid addid srefmon: egen N_ParsWithCollege=total(temp_college)
by suid addid srefmon: egen N_CollegeGradPars=total(MidGenPar_CollegeGrad)
by suid addid srefmon: egen N_ParsEmployed=total(MidGenPar_Employed)
by suid addid srefmon: egen N_ParsPotentialDep=total(MidGenPar_PotentialDep)


gen AnyParsPotentialDeps=(N_ParsPotentialDep>0)
gen AnySingleMom = (N_SingleMom>0)
gen AnySingleNoCollegeMom=(N_SingleNoCollegeMom>0)
gen NoParsWithCollege = (N_ParsWithCollege==0)
gen NoCollegeGradPars = (N_CollegeGradPars==0)
gen AnyParsEmployed = (N_ParsEmployed>0)

drop temp_college


gen RefPerson=(errp==1 | errp==2)
sort suid addid srefmon
by suid addid srefmon: egen N_RefPerson=total(RefPerson)
gen MidGenHasRefPerson=(N_RefPerson>0)



tostring suid, replace
merge m:1 suid entry pnum using WellBeingVars1992_w4 


keep if _merge==3
drop _merge




gen panel=1992
save OrganizedMidGenParentData1992_w4, replace


** Extract information at household-month level about MAXIMUM amount of EITC eligibility
** for which any mid-gen parent is available.  The goal is to make this info ready 
** for merging with grandparents who live in those same households.
** Also extract info about whether the middle generation includes any single mom,
** or any single mom with no college education, any employed parents, a HH ref person, etc
** SPRING 2022 UPDATE: Do for three measures of EITC: legislated max EITC using lagged tax 
** year, legislated max EITC using current tax year, simulated EITC 


keep panel swave suid addid srefmon r_MaxEITC_thou r_ref_MaxEITC currentyear_r_MaxEITC_thou currentyear_r_ref_MaxEITC taxsim_eic_thou xocah AnySingleMom AnySingleNoCollegeMom NoParsWithCollege NoCollegeGradPars AnyParsEmployed MidGenHasRefPerson  AnyParsPotentialDeps r_MaxFedEITC_thou ts_eic_fed
sort suid addid srefmon
by suid addid srefmon: egen r_MaxEITC_MidGenMax=max(r_MaxEITC_thou)
by suid addid srefmon: egen r_ref_MaxEITC_MidGenMax=max(r_ref_MaxEITC)
by suid addid srefmon: egen currentyear_r_MaxEITC_MidGenMax=max(currentyear_r_MaxEITC_thou)
by suid addid srefmon: egen taxsim_eic_MidGenMax=max(taxsim_eic_thou)
by suid addid srefmon: egen r_MaxFedEITC_MidGenMax=max(r_MaxFedEITC_thou)
by suid addid srefmon: egen taxsim_eic_Fed_MidGenMax=max(ts_eic_fed)

** Only keep observations of individuals whose EITC eligibility is equal to the max EITC
** eligibility of anyone in the middle generation  (just a way of keeping one record per HH)
keep if r_MaxEITC_MidGenMax == r_MaxEITC_thou


** For later construction of a summary stats table, distinguish between cases of 1 EITC-eligible
** child and 2+ EITC-eligible children of the middle-generation parent who is eligible for the largest
** EITC amount
gen xocah_1or2plus=.
replace xocah_1or2plus = 1 if xocah==1
replace xocah_1or2plus = 2 if xocah>1 

drop xocah* r_MaxEITC_thou r_ref_MaxEITC currentyear_r_MaxEITC_thou taxsim_eic_thou ts_eic_fed
duplicates drop

duplicates report suid addid srefmon


save MaxMidGenEITC1992_w4, replace
clear



** Another way to count EITC resources in the middle generation is to sum up amounts received
** by different mid-gen parents.  You don't want to double-count EITC eligibility of a husband
** and wife, but you could assume that all mothers are separate filers and sum up all EITC
** amounts of the different mid-gen mothers.  
use OrganizedMidGenParentData1992_w4

keep panel swave suid addid srefmon r_MaxEITC_thou r_ref_MaxEITC currentyear_r_MaxEITC_thou currentyear_r_ref_MaxEITC taxsim_eic_thou PotentialMidGenMom r_MaxFedEITC_thou ts_eic_fed
keep if PotentialMidGenMom==1
duplicates report suid addid srefmon
sort suid addid srefmon
by suid addid srefmon: egen Sum_MaxEITC_MidGenMoms = sum(r_MaxEITC_thou)
by suid addid srefmon: egen Sum_ref_MaxEITC_MidGenMoms = sum(r_ref_MaxEITC)
by suid addid srefmon: egen Sum_cy_MaxEITC_MidGenMoms = sum(currentyear_r_MaxEITC_thou)
by suid addid srefmon: egen Sum_cy_ref_MaxEITC_MidGenMoms = sum(currentyear_r_ref_MaxEITC)
by suid addid srefmon: egen Sum_taxsim_eic_MidGenMoms = sum(taxsim_eic_thou)
by suid addid srefmon: egen Sum_MaxFedEITC_MidGenMoms = sum(r_MaxFedEITC_thou)
by suid addid srefmon: egen Sum_taxsim_eic_Fed_MidGenMoms = sum(ts_eic_fed)

keep panel swave suid addid  srefmon Sum_MaxEITC_MidGenMoms Sum_ref_MaxEITC_MidGenMoms Sum_cy_MaxEITC_MidGenMoms Sum_cy_ref_MaxEITC_MidGenMoms Sum_taxsim_eic_MidGenMoms Sum_MaxFedEITC_MidGenMoms Sum_taxsim_eic_Fed_MidGenMoms
duplicates drop
save SumMidGenEITC_1992_w4, replace
clear



*******************
** 1992
*******************

***********
** Wave 6
***********


use  MidGenParentData1992_w6
** Merge in count of how many people in household identified this person as his/her mother
merge 1:1 suid addid pnum srefmon using KidCount1992_w6
assert _merge~=1
keep if _merge==3
drop _merge


** EXPERIMENTING: Try a version where you DON'T subtract one from year
** before doing EITC calculations
gen EITC_calc_year = (1900 + rhcalyr)
gen xocah = Mod_DepCount
quietly do EITC_Calculator.do
drop MaxFedEITC
drop MaxStateEITC
drop StateEITC_NonRefund
rename r_MaxEITC currentyear_r_MaxEITC
rename r_MaxEITC_thou currentyear_r_MaxEITC_thou
rename r_MaxFedEITC_thou currentyear_r_MaxFedEITC_thou
rename r_MaxStateEITC_thou  currentyear_r_MaxStateEITC_thou
rename r_ref_MaxEITC  currentyear_r_ref_MaxEITC
drop real
drop EITC_calc_year xocah


** Now back to original way of calculating EITC amounts
gen EITC_calc_year = (1900+rhcalyr)-1
gen xocah = Mod_DepCount
quietly do EITC_Calculator.do



**********************************************************
** Now merge in simulated EITC amount (average for cell defined
** in terms of YearOfTaxCalc, tfipsst, Married, KidsForSimulated
gen YearOfTaxCalc = (1900+rhcalyr)-1
gen KidsForSimulated = Mod_DepCount
replace KidsForSimulated = 3 if KidsForSimulated>3
merge m:1 YearOfTaxCalc tfipsst Married KidsForSimulated using SimulatedEITC
drop if _merge==2
drop _merge
gen taxsim_eic_thou = (ts_eic_fed + ts_eic_state)/1000
*******************************************************************



rename Married MidGenPar_Married
rename Employed MidGenPar_Employed
rename HSGrad MidGenPar_HSGrad
rename SomeCollege MidGenPar_SomeCollege
rename CollegeGrad MidGenPar_CollegeGrad
rename White MidGenPar_White
rename PotentialDep MidGenPar_PotentialDep

gen SingleMom = (Female==1 & MidGenPar_Married==0)
gen SingleNoCollegeMom = (Female==1 & MidGenPar_Married==0 & MidGenPar_SomeCollege==0 & MidGenPar_CollegeGrad==0)
gen temp_college = (MidGenPar_SomeCollege==1 | MidGenPar_CollegeGrad==1)


sort suid addid srefmon
by suid addid srefmon: egen N_SingleMom=total(SingleMom)
by suid addid srefmon: egen N_SingleNoCollegeMom = total(SingleNoCollegeMom)
by suid addid srefmon: egen N_ParsWithCollege=total(temp_college)
by suid addid srefmon: egen N_CollegeGradPars=total(MidGenPar_CollegeGrad)
by suid addid srefmon: egen N_ParsEmployed=total(MidGenPar_Employed)
by suid addid srefmon: egen N_ParsPotentialDep=total(MidGenPar_PotentialDep)


gen AnyParsPotentialDeps=(N_ParsPotentialDep>0)
gen AnySingleMom = (N_SingleMom>0)
gen AnySingleNoCollegeMom=(N_SingleNoCollegeMom>0)
gen NoParsWithCollege = (N_ParsWithCollege==0)
gen NoCollegeGradPars = (N_CollegeGradPars==0)
gen AnyParsEmployed = (N_ParsEmployed>0)

drop temp_college


gen RefPerson=(errp==1 | errp==2)
sort suid addid srefmon
by suid addid srefmon: egen N_RefPerson=total(RefPerson)
gen MidGenHasRefPerson=(N_RefPerson>0)


tostring suid, replace
merge m:1 suid entry pnum using WellBeingVars1992_w6 


keep if _merge==3
drop _merge





*******************************************************************************
** Merge in data on receipt of childcare from a grandparent 

merge m:1 suid entry pnum using ChildCareVars_1992_w6
drop if _merge==2
drop _merge 

sort suid addid srefmon
by suid addid srefmon: egen N_GetCCFromGP=total(GetCCFromGP)
gen AnyParsGetCCFromGP = (N_GetCCFromGP>0)

gen panel=1992
save OrganizedMidGenParentData1992_w6, replace


** Extract information at household-month level about MAXIMUM amount of EITC eligibility
** for which any mid-gen parent is available.  The goal is to make this info ready 
** for merging with grandparents who live in those same households.
** Also extract info about whether the middle generation includes any single mom,
** or any single mom with no college education, any employed parents, a HH ref person, etc
** SPRING 2022 UPDATE: Do for three measures of EITC: legislated max EITC using lagged tax 
** year, legislated max EITC using current tax year, simulated EITC 


keep panel swave suid addid srefmon r_MaxEITC_thou r_ref_MaxEITC currentyear_r_MaxEITC_thou currentyear_r_ref_MaxEITC taxsim_eic_thou xocah AnySingleMom AnySingleNoCollegeMom NoParsWithCollege NoCollegeGradPars AnyParsEmployed MidGenHasRefPerson  AnyParsPotentialDeps r_MaxFedEITC_thou ts_eic_fed AnyParsGetCCFromGP
sort suid addid srefmon
by suid addid srefmon: egen r_MaxEITC_MidGenMax=max(r_MaxEITC_thou)
by suid addid srefmon: egen r_ref_MaxEITC_MidGenMax=max(r_ref_MaxEITC)
by suid addid srefmon: egen currentyear_r_MaxEITC_MidGenMax=max(currentyear_r_MaxEITC_thou)
by suid addid srefmon: egen taxsim_eic_MidGenMax=max(taxsim_eic_thou)
by suid addid srefmon: egen r_MaxFedEITC_MidGenMax=max(r_MaxFedEITC_thou)
by suid addid srefmon: egen taxsim_eic_Fed_MidGenMax=max(ts_eic_fed)

** Only keep observations of individuals whose EITC eligibility is equal to the max EITC
** eligibility of anyone in the middle generation  (just a way of keeping one record per HH)
keep if r_MaxEITC_MidGenMax == r_MaxEITC_thou

** For later construction of a summary stats table, distinguish between cases of 1 EITC-eligible
** child and 2+ EITC-eligible children of the middle-generation parent who is eligible for the largest
** EITC amount
gen xocah_1or2plus=.
replace xocah_1or2plus = 1 if xocah==1
replace xocah_1or2plus = 2 if xocah>1 

drop xocah r_MaxEITC_thou r_ref_MaxEITC currentyear_r_MaxEITC_thou taxsim_eic_thou ts_eic_fed
duplicates drop

duplicates report suid addid srefmon

save MaxMidGenEITC1992_w6, replace
clear



** Another way to count EITC resources in the middle generation is to sum up amounts received
** by different mid-gen parents.  You don't want to double-count EITC eligibility of a husband
** and wife, but you could assume that all mothers are separate filers and sum up all EITC
** amounts of the different mid-gen mothers.  
use OrganizedMidGenParentData1992_w6

keep panel swave suid addid srefmon r_MaxEITC_thou r_ref_MaxEITC currentyear_r_MaxEITC_thou currentyear_r_ref_MaxEITC taxsim_eic_thou PotentialMidGenMom r_MaxFedEITC_thou ts_eic_fed
keep if PotentialMidGenMom==1
duplicates report suid addid srefmon
sort suid addid srefmon
by suid addid srefmon: egen Sum_MaxEITC_MidGenMoms = sum(r_MaxEITC_thou)
by suid addid srefmon: egen Sum_ref_MaxEITC_MidGenMoms = sum(r_ref_MaxEITC)
by suid addid srefmon: egen Sum_cy_MaxEITC_MidGenMoms = sum(currentyear_r_MaxEITC_thou)
by suid addid srefmon: egen Sum_cy_ref_MaxEITC_MidGenMoms = sum(currentyear_r_ref_MaxEITC)
by suid addid srefmon: egen Sum_taxsim_eic_MidGenMoms = sum(taxsim_eic_thou)
by suid addid srefmon: egen Sum_MaxFedEITC_MidGenMoms = sum(r_MaxFedEITC_thou)
by suid addid srefmon: egen Sum_taxsim_eic_Fed_MidGenMoms = sum(ts_eic_fed)

keep panel swave suid addid srefmon Sum_MaxEITC_MidGenMoms Sum_ref_MaxEITC_MidGenMoms Sum_cy_MaxEITC_MidGenMoms Sum_cy_ref_MaxEITC_MidGenMoms Sum_taxsim_eic_MidGenMoms Sum_MaxFedEITC_MidGenMoms Sum_taxsim_eic_Fed_MidGenMoms
duplicates drop
save SumMidGenEITC_1992_w6, replace
clear






*******************
** 1993
*******************

***********
** Wave 3
***********

use  MidGenParentData1993_w3
** Merge in count of how many people in household identified this person as his/her mother
merge 1:1 suid addid pnum srefmon using KidCount1993_w3
assert _merge~=1
keep if _merge==3
drop _merge


** EXPERIMENTING: Try a version where you DON'T subtract one from year
** before doing EITC calculations
gen EITC_calc_year = (1900 + rhcalyr)
gen xocah = Mod_DepCount
quietly do EITC_Calculator.do
drop MaxFedEITC
drop MaxStateEITC
drop StateEITC_NonRefund
rename r_MaxEITC currentyear_r_MaxEITC
rename r_MaxEITC_thou currentyear_r_MaxEITC_thou
rename r_MaxFedEITC_thou currentyear_r_MaxFedEITC_thou
rename r_MaxStateEITC_thou  currentyear_r_MaxStateEITC_thou
rename r_ref_MaxEITC  currentyear_r_ref_MaxEITC
drop real
drop EITC_calc_year xocah


** Now back to original way of calculating EITC amounts
gen EITC_calc_year = (1900+rhcalyr)-1
gen xocah = Mod_DepCount
quietly do EITC_Calculator.do

** Now merge in simulated EITC amount (average for cell defined
** in terms of YearOfTaxCalc, tfipsst, Married, KidsForSimulated
gen YearOfTaxCalc = (1900+rhcalyr)-1
gen KidsForSimulated = Mod_DepCount
replace KidsForSimulated = 3 if KidsForSimulated>3
merge m:1 YearOfTaxCalc tfipsst Married KidsForSimulated using SimulatedEITC
drop if _merge==2
drop _merge
gen taxsim_eic_thou = (ts_eic_fed + ts_eic_state)/1000


rename Married MidGenPar_Married
rename Employed MidGenPar_Employed
rename HSGrad MidGenPar_HSGrad
rename SomeCollege MidGenPar_SomeCollege
rename CollegeGrad MidGenPar_CollegeGrad
rename White MidGenPar_White
rename PotentialDep MidGenPar_PotentialDep

gen SingleMom = (Female==1 & MidGenPar_Married==0)
gen SingleNoCollegeMom = (Female==1 & MidGenPar_Married==0 & MidGenPar_SomeCollege==0 & MidGenPar_CollegeGrad==0)
gen temp_college = (MidGenPar_SomeCollege==1 | MidGenPar_CollegeGrad==1)

sort suid addid srefmon
by suid addid srefmon: egen N_SingleMom=total(SingleMom)
by suid addid srefmon: egen N_SingleNoCollegeMom = total(SingleNoCollegeMom)
by suid addid srefmon: egen N_ParsWithCollege=total(temp_college)
by suid addid srefmon: egen N_CollegeGradPars=total(MidGenPar_CollegeGrad)
by suid addid srefmon: egen N_ParsEmployed=total(MidGenPar_Employed)
by suid addid srefmon: egen N_ParsPotentialDep=total(MidGenPar_PotentialDep)

gen AnyParsPotentialDeps=(N_ParsPotentialDep>0)
gen AnySingleMom = (N_SingleMom>0)
gen AnySingleNoCollegeMom=(N_SingleNoCollegeMom>0)
gen NoParsWithCollege = (N_ParsWithCollege==0)
gen NoCollegeGradPars = (N_CollegeGradPars==0)
gen AnyParsEmployed = (N_ParsEmployed>0)

drop temp_college

gen RefPerson=(errp==1 | errp==2)
sort suid addid srefmon
by suid addid srefmon: egen N_RefPerson=total(RefPerson)
gen MidGenHasRefPerson=(N_RefPerson>0)


tostring suid, replace
merge m:1 suid entry pnum using WellBeingVars1993_w3 


keep if _merge==3
drop _merge



*******************************************************************************
** Merge in data on receipt of childcare from a grandparent 

merge m:1 suid entry pnum using ChildCareVars_1993_w3
drop if _merge==2
drop _merge 

sort suid addid srefmon
by suid addid srefmon: egen N_GetCCFromGP=total(GetCCFromGP)
gen AnyParsGetCCFromGP = (N_GetCCFromGP>0)


gen panel=1993
save OrganizedMidGenParentData1993_w3, replace




** Extract information at household-month level about MAXIMUM amount of EITC eligibility
** for which any mid-gen parent is available.  The goal is to make this info ready 
** for merging with grandparents who live in those same households.
** Also extract info about whether the middle generation includes any single mom,
** or any single mom with no college education, any employed parents, a HH ref person, etc
** SPRING 2022 UPDATE: Do for three measures of EITC: legislated max EITC using lagged tax 
** year, legislated max EITC using current tax year, simulated EITC 

keep panel swave suid addid srefmon r_MaxEITC_thou r_ref_MaxEITC currentyear_r_MaxEITC_thou currentyear_r_ref_MaxEITC taxsim_eic_thou xocah AnySingleMom AnySingleNoCollegeMom NoParsWithCollege NoCollegeGradPars AnyParsEmployed MidGenHasRefPerson  AnyParsPotentialDeps r_MaxFedEITC_thou ts_eic_fed AnyParsGetCCFromGP
sort suid addid srefmon
by suid addid srefmon: egen r_MaxEITC_MidGenMax=max(r_MaxEITC_thou)
by suid addid srefmon: egen r_ref_MaxEITC_MidGenMax=max(r_ref_MaxEITC)
by suid addid srefmon: egen currentyear_r_MaxEITC_MidGenMax=max(currentyear_r_MaxEITC_thou)
by suid addid srefmon: egen taxsim_eic_MidGenMax=max(taxsim_eic_thou)
by suid addid srefmon: egen r_MaxFedEITC_MidGenMax=max(r_MaxFedEITC_thou)
by suid addid srefmon: egen taxsim_eic_Fed_MidGenMax=max(ts_eic_fed)

** Only keep observations of individuals whose EITC eligibility is equal to the max EITC
** eligibility of anyone in the middle generation  (just a way of keeping one record per HH)
keep if r_MaxEITC_MidGenMax == r_MaxEITC_thou
duplicates drop

** For later construction of a summary stats table, distinguish between cases of 1 EITC-eligible
** child and 2+ EITC-eligible children of the middle-generation parent who is eligible for the largest
** EITC amount
gen xocah_1or2plus=.
replace xocah_1or2plus = 1 if xocah==1
replace xocah_1or2plus = 2 if xocah>1 

drop xocah r_MaxEITC_thou r_ref_MaxEITC currentyear_r_MaxEITC_thou taxsim_eic_thou ts_eic_fed
duplicates drop

duplicates report suid addid srefmon

save MaxMidGenEITC1993_w3, replace
clear



** Another way to count EITC resources in the middle generation is to sum up amounts received
** by different mid-gen parents.  You don't want to double-count EITC eligibility of a husband
** and wife, but you could assume that all mothers are separate filers and sum up all EITC
** amounts of the different mid-gen mothers.  
use OrganizedMidGenParentData1993_w3

keep panel swave suid addid srefmon r_MaxEITC_thou r_ref_MaxEITC currentyear_r_MaxEITC_thou currentyear_r_ref_MaxEITC taxsim_eic_thou PotentialMidGenMom r_MaxFedEITC_thou ts_eic_fed
keep if PotentialMidGenMom==1
duplicates report suid addid srefmon
sort suid addid srefmon
by suid addid srefmon: egen Sum_MaxEITC_MidGenMoms = sum(r_MaxEITC_thou)
by suid addid srefmon: egen Sum_ref_MaxEITC_MidGenMoms = sum(r_ref_MaxEITC)
by suid addid srefmon: egen Sum_cy_MaxEITC_MidGenMoms = sum(currentyear_r_MaxEITC_thou)
by suid addid srefmon: egen Sum_cy_ref_MaxEITC_MidGenMoms = sum(currentyear_r_ref_MaxEITC)
by suid addid srefmon: egen Sum_taxsim_eic_MidGenMoms = sum(taxsim_eic_thou)
by suid addid srefmon: egen Sum_MaxFedEITC_MidGenMoms = sum(r_MaxFedEITC_thou)
by suid addid srefmon: egen Sum_taxsim_eic_Fed_MidGenMoms = sum(ts_eic_fed)

keep panel swave suid addid  srefmon Sum_MaxEITC_MidGenMoms Sum_ref_MaxEITC_MidGenMoms Sum_cy_MaxEITC_MidGenMoms Sum_cy_ref_MaxEITC_MidGenMoms  Sum_taxsim_eic_MidGenMoms Sum_MaxFedEITC_MidGenMoms Sum_taxsim_eic_Fed_MidGenMoms
duplicates drop
save SumMidGenEITC_1993_w3, replace
clear




***********
** Wave 6
***********

use MidGenParentData1993_w6
** Merge in count of how many people in household identified this person as his/her mother
merge 1:1 suid addid pnum srefmon using KidCount1993_w6
assert _merge~=1
keep if _merge==3
drop _merge


** EXPERIMENTING: Try a version where you DON'T subtract one from year
** before doing EITC calculations
gen EITC_calc_year = (1900 + rhcalyr)
gen xocah = Mod_DepCount
quietly do EITC_Calculator.do
drop MaxFedEITC
drop MaxStateEITC
drop StateEITC_NonRefund
rename r_MaxEITC currentyear_r_MaxEITC
rename r_MaxEITC_thou currentyear_r_MaxEITC_thou
rename r_MaxFedEITC_thou currentyear_r_MaxFedEITC_thou
rename r_MaxStateEITC_thou  currentyear_r_MaxStateEITC_thou
rename r_ref_MaxEITC  currentyear_r_ref_MaxEITC
drop real
drop EITC_calc_year xocah

gen EITC_calc_year = (1900+rhcalyr)-1
gen xocah = Mod_DepCount
quietly do EITC_Calculator.do


** Now merge in simulated EITC amount (average for cell defined
** in terms of YearOfTaxCalc, tfipsst, Married, KidsForSimulated
gen YearOfTaxCalc = (1900+rhcalyr)-1
gen KidsForSimulated = Mod_DepCount
replace KidsForSimulated = 3 if KidsForSimulated>3
merge m:1 YearOfTaxCalc tfipsst Married KidsForSimulated using SimulatedEITC
drop if _merge==2
drop _merge
gen taxsim_eic_thou = (ts_eic_fed + ts_eic_state)/1000

rename Married MidGenPar_Married
rename Employed MidGenPar_Employed
rename HSGrad MidGenPar_HSGrad
rename SomeCollege MidGenPar_SomeCollege
rename CollegeGrad MidGenPar_CollegeGrad
rename White MidGenPar_White
rename PotentialDep MidGenPar_PotentialDep


gen SingleMom = (Female==1 & MidGenPar_Married==0)
gen SingleNoCollegeMom = (Female==1 & MidGenPar_Married==0 & MidGenPar_SomeCollege==0 & MidGenPar_CollegeGrad==0)
gen temp_college = (MidGenPar_SomeCollege==1 | MidGenPar_CollegeGrad==1)

sort suid addid srefmon
by suid addid srefmon: egen N_SingleMom=total(SingleMom)
by suid addid srefmon: egen N_SingleNoCollegeMom = total(SingleNoCollegeMom)
by suid addid srefmon: egen N_ParsWithCollege=total(temp_college)
by suid addid srefmon: egen N_CollegeGradPars=total(MidGenPar_CollegeGrad)
by suid addid srefmon: egen N_ParsEmployed=total(MidGenPar_Employed)
by suid addid srefmon: egen N_ParsPotentialDep=total(MidGenPar_PotentialDep)

gen AnyParsPotentialDeps=(N_ParsPotentialDep>0)
gen AnySingleMom = (N_SingleMom>0)
gen AnySingleNoCollegeMom=(N_SingleNoCollegeMom>0)
gen NoParsWithCollege = (N_ParsWithCollege==0)
gen NoCollegeGradPars = (N_CollegeGradPars==0)
gen AnyParsEmployed = (N_ParsEmployed>0)
drop temp_college

gen RefPerson=(errp==1 | errp==2)
sort suid addid srefmon
by suid addid srefmon: egen N_RefPerson=total(RefPerson)
gen MidGenHasRefPerson=(N_RefPerson>0)



tostring suid, replace
merge m:1 suid entry pnum using WellBeingVars1993_w6 


keep if _merge==3
drop _merge


*******************************************************************************
** Merge in data on receipt of childcare from a grandparent 

merge m:1 suid entry pnum using ChildCareVars_1993_w6
drop if _merge==2
drop _merge 

sort suid addid srefmon
by suid addid srefmon: egen N_GetCCFromGP=total(GetCCFromGP)
gen AnyParsGetCCFromGP = (N_GetCCFromGP>0)


gen panel=1993
save OrganizedMidGenParentData1993_w6, replace




** Extract information at household-month level about MAXIMUM amount of EITC eligibility
** for which any mid-gen parent is available.  The goal is to make this info ready 
** for merging with grandparents who live in those same households.
** Also extract info about whether the middle generation includes any single mom,
** or any single mom with no college education

keep panel swave suid addid srefmon r_MaxEITC_thou r_ref_MaxEITC currentyear_r_MaxEITC_thou currentyear_r_ref_MaxEITC taxsim_eic_thou xocah AnySingleMom AnySingleNoCollegeMom NoParsWithCollege NoCollegeGradPars AnyParsEmployed MidGenHasRefPerson AnyParsPotentialDeps r_MaxFedEITC_thou ts_eic_fed AnyParsGetCCFromGP
sort suid addid srefmon
by suid addid srefmon: egen r_MaxEITC_MidGenMax=max(r_MaxEITC_thou)
by suid addid srefmon: egen r_ref_MaxEITC_MidGenMax=max(r_ref_MaxEITC)
by suid addid srefmon: egen currentyear_r_MaxEITC_MidGenMax=max(currentyear_r_MaxEITC_thou)
by suid addid srefmon: egen taxsim_eic_MidGenMax=max(taxsim_eic_thou)
by suid addid srefmon: egen r_MaxFedEITC_MidGenMax=max(r_MaxFedEITC_thou)
by suid addid srefmon: egen taxsim_eic_Fed_MidGenMax=max(ts_eic_fed)

** Only keep observations of individuals whose EITC eligibility is equal to the max EITC
** eligibility of anyone in the middle generation
keep if r_MaxEITC_MidGenMax == r_MaxEITC_thou
duplicates drop

** For later construction of a summary stats table, distinguish between cases of 1 EITC-eligible
** child and 2+ EITC-eligible children of the middle-generation parent who is eligible for the largest
** EITC amount
gen xocah_1or2plus=.
replace xocah_1or2plus = 1 if xocah==1
replace xocah_1or2plus = 2 if xocah>1 

drop xocah r_MaxEITC_thou r_ref_MaxEITC currentyear_r_MaxEITC_thou taxsim_eic_thou ts_eic_fed
duplicates drop



save MaxMidGenEITC1993_w6, replace
clear




** Another way to count EITC resources in the middle generation is to sum up amounts received
** by different mid-gen parents.  You don't want to double-count EITC eligibility of a husband
** and wife, but you could assume that all mothers are separate filers and sum up all EITC
** amounts of the different mid-gen mothers.  
use OrganizedMidGenParentData1993_w6

keep panel swave suid addid  srefmon r_MaxEITC_thou r_ref_MaxEITC currentyear_r_MaxEITC_thou currentyear_r_ref_MaxEITC taxsim_eic_thou PotentialMidGenMom r_MaxFedEITC_thou ts_eic_fed
keep if PotentialMidGenMom==1
duplicates report suid addid srefmon
sort suid addid srefmon
by suid addid srefmon: egen Sum_MaxEITC_MidGenMoms = sum(r_MaxEITC_thou)
by suid addid srefmon: egen Sum_ref_MaxEITC_MidGenMoms = sum(r_ref_MaxEITC)
by suid addid srefmon: egen Sum_cy_MaxEITC_MidGenMoms = sum(currentyear_r_MaxEITC_thou)
by suid addid srefmon: egen Sum_cy_ref_MaxEITC_MidGenMoms = sum(currentyear_r_ref_MaxEITC)
by suid addid srefmon: egen Sum_taxsim_eic_MidGenMoms = sum(taxsim_eic_thou)
by suid addid srefmon: egen Sum_MaxFedEITC_MidGenMoms = sum(r_MaxFedEITC_thou)
by suid addid srefmon: egen Sum_taxsim_eic_Fed_MidGenMoms = sum(ts_eic_fed)

keep panel swave suid addid srefmon Sum_MaxEITC_MidGenMoms Sum_ref_MaxEITC_MidGenMoms Sum_cy_MaxEITC_MidGenMoms Sum_cy_ref_MaxEITC_MidGenMoms Sum_taxsim_eic_MidGenMoms Sum_MaxFedEITC_MidGenMoms Sum_taxsim_eic_Fed_MidGenMoms
duplicates drop
save SumMidGenEITC_1993_w6, replace
clear






***********
** Wave 7
***********

use  MidGenParentData1993_w7
** Merge in count of how many people in household identified this person as his/her mother
merge 1:1 suid addid pnum srefmon using KidCount1993_w7
assert _merge~=1
keep if _merge==3
drop _merge


** EXPERIMENTING: Try a version where you DON'T subtract one from year
** before doing EITC calculations
gen EITC_calc_year = (1900 + rhcalyr)
gen xocah = Mod_DepCount
quietly do EITC_Calculator.do
drop MaxFedEITC
drop MaxStateEITC
drop StateEITC_NonRefund
rename r_MaxEITC currentyear_r_MaxEITC
rename r_MaxEITC_thou currentyear_r_MaxEITC_thou
rename r_MaxFedEITC_thou currentyear_r_MaxFedEITC_thou
rename r_MaxStateEITC_thou  currentyear_r_MaxStateEITC_thou
rename r_ref_MaxEITC  currentyear_r_ref_MaxEITC
drop real
drop EITC_calc_year xocah

gen EITC_calc_year = (1900+rhcalyr)-1
gen xocah = Mod_DepCount
quietly do EITC_Calculator.do


** Now merge in simulated EITC amount (average for cell defined
** in terms of YearOfTaxCalc, tfipsst, Married, KidsForSimulated
gen YearOfTaxCalc = (1900+rhcalyr)-1
gen KidsForSimulated = Mod_DepCount
replace KidsForSimulated = 3 if KidsForSimulated>3
merge m:1 YearOfTaxCalc tfipsst Married KidsForSimulated using SimulatedEITC
drop if _merge==2
drop _merge
gen taxsim_eic_thou = (ts_eic_fed + ts_eic_state)/1000

rename Married MidGenPar_Married
rename Employed MidGenPar_Employed
rename HSGrad MidGenPar_HSGrad
rename SomeCollege MidGenPar_SomeCollege
rename CollegeGrad MidGenPar_CollegeGrad
rename White MidGenPar_White
rename PotentialDep MidGenPar_PotentialDep


gen SingleMom = (Female==1 & MidGenPar_Married==0)
gen SingleNoCollegeMom = (Female==1 & MidGenPar_Married==0 & MidGenPar_SomeCollege==0 & MidGenPar_CollegeGrad==0)
gen temp_college = (MidGenPar_SomeCollege==1 | MidGenPar_CollegeGrad==1)

sort suid addid srefmon
by suid addid srefmon: egen N_SingleMom=total(SingleMom)
by suid addid srefmon: egen N_SingleNoCollegeMom = total(SingleNoCollegeMom)
by suid addid srefmon: egen N_ParsWithCollege=total(temp_college)
by suid addid srefmon: egen N_CollegeGradPars=total(MidGenPar_CollegeGrad)
by suid addid srefmon: egen N_ParsEmployed=total(MidGenPar_Employed)
by suid addid srefmon: egen N_ParsPotentialDep=total(MidGenPar_PotentialDep)

gen AnyParsPotentialDeps=(N_ParsPotentialDep>0)
gen AnySingleMom = (N_SingleMom>0)
gen AnySingleNoCollegeMom=(N_SingleNoCollegeMom>0)
gen NoParsWithCollege = (N_ParsWithCollege==0)
gen NoCollegeGradPars = (N_CollegeGradPars==0)
gen AnyParsEmployed = (N_ParsEmployed>0)
drop temp_college

gen RefPerson=(errp==1 | errp==2)
sort suid addid srefmon
by suid addid srefmon: egen N_RefPerson=total(RefPerson)
gen MidGenHasRefPerson=(N_RefPerson>0)



tostring suid, replace
merge m:1 suid entry pnum using WellBeingVars1993_w7

keep if _merge==3
drop _merge

gen panel=1993
save OrganizedMidGenParentData1993_w7, replace




** Extract information at household-month level about MAXIMUM amount of EITC eligibility
** for which any mid-gen parent is available.  The goal is to make this info ready 
** for merging with grandparents who live in those same households.
** Also extract info about whether the middle generation includes any single mom,
** or any single mom with no college education

keep panel swave suid addid srefmon r_MaxEITC_thou r_ref_MaxEITC currentyear_r_MaxEITC_thou taxsim_eic_thou xocah AnySingleMom AnySingleNoCollegeMom NoParsWithCollege NoCollegeGradPars AnyParsEmployed MidGenHasRefPerson AnyParsPotentialDeps r_MaxFedEITC_thou ts_eic_fed
sort suid addid srefmon
by suid addid srefmon: egen r_MaxEITC_MidGenMax=max(r_MaxEITC_thou)
by suid addid srefmon: egen r_ref_MaxEITC_MidGenMax=max(r_ref_MaxEITC)
by suid addid srefmon: egen currentyear_r_MaxEITC_MidGenMax=max(currentyear_r_MaxEITC_thou)
by suid addid srefmon: egen taxsim_eic_MidGenMax=max(taxsim_eic_thou)
by suid addid srefmon: egen r_MaxFedEITC_MidGenMax=max(r_MaxFedEITC_thou)
by suid addid srefmon: egen taxsim_eic_Fed_MidGenMax=max(ts_eic_fed)

** Only keep observations of individuals whose EITC eligibility is equal to the max EITC
** eligibility of anyone in the middle generation
keep if r_MaxEITC_MidGenMax == r_MaxEITC_thou
duplicates drop

** For later construction of a summary stats table, distinguish between cases of 1 EITC-eligible
** child and 2+ EITC-eligible children of the middle-generation parent who is eligible for the largest
** EITC amount
gen xocah_1or2plus=.
replace xocah_1or2plus = 1 if xocah==1
replace xocah_1or2plus = 2 if xocah>1 

drop xocah r_MaxEITC_thou r_ref_MaxEITC currentyear_r_MaxEITC_thou taxsim_eic_thou ts_eic_fed
duplicates drop



save MaxMidGenEITC1993_w7, replace
clear




** Another way to count EITC resources in the middle generation is to sum up amounts received
** by different mid-gen parents.  You don't want to double-count EITC eligibility of a husband
** and wife, but you could assume that all mothers are separate filers and sum up all EITC
** amounts of the different mid-gen mothers.  
use OrganizedMidGenParentData1993_w7

keep panel swave suid addid srefmon r_MaxEITC_thou r_ref_MaxEITC currentyear_r_MaxEITC_thou currentyear_r_ref_MaxEITC taxsim_eic_thou PotentialMidGenMom r_MaxFedEITC_thou ts_eic_fed
keep if PotentialMidGenMom==1
duplicates report suid addid srefmon
sort suid addid srefmon
by suid addid srefmon: egen Sum_MaxEITC_MidGenMoms = sum(r_MaxEITC_thou)
by suid addid srefmon: egen Sum_ref_MaxEITC_MidGenMoms = sum(r_ref_MaxEITC)
by suid addid srefmon: egen Sum_cy_MaxEITC_MidGenMoms = sum(currentyear_r_MaxEITC_thou)
by suid addid srefmon: egen Sum_cy_ref_MaxEITC_MidGenMoms = sum(currentyear_r_ref_MaxEITC)
by suid addid srefmon: egen Sum_taxsim_eic_MidGenMoms = sum(taxsim_eic_thou)
by suid addid srefmon: egen Sum_MaxFedEITC_MidGenMoms = sum(r_MaxFedEITC_thou)
by suid addid srefmon: egen Sum_taxsim_eic_Fed_MidGenMoms = sum(ts_eic_fed)

keep panel swave suid addid srefmon Sum_MaxEITC_MidGenMoms Sum_ref_MaxEITC_MidGenMoms Sum_cy_MaxEITC_MidGenMoms Sum_cy_ref_MaxEITC_MidGenMoms Sum_taxsim_eic_MidGenMoms Sum_MaxFedEITC_MidGenMoms Sum_taxsim_eic_Fed_MidGenMoms
duplicates drop
save SumMidGenEITC_1993_w7, replace
clear



*******************
** 1996
*******************

***********
** Wave 3
***********

use  MidGenParentData1996_w3
** Merge in count of how many people in household identified this person as his/her mother
merge 1:1 ssuid epppnum srefmon using KidCount1996_w3
assert _merge~=1
keep if _merge==3
drop _merge


** EXPERIMENTING: Try a version where you DON'T subtract one from year
** before doing EITC calculations
gen EITC_calc_year = rhcalyr
gen xocah = DepCount
quietly do EITC_Calculator.do
drop MaxFedEITC
drop MaxStateEITC
drop StateEITC_NonRefund
rename r_MaxEITC currentyear_r_MaxEITC
rename r_MaxEITC_thou currentyear_r_MaxEITC_thou
rename r_MaxFedEITC_thou currentyear_r_MaxFedEITC_thou
rename r_MaxStateEITC_thou  currentyear_r_MaxStateEITC_thou
rename r_ref_MaxEITC  currentyear_r_ref_MaxEITC
drop real
drop EITC_calc_year xocah

gen EITC_calc_year = rhcalyr-1
gen xocah = DepCount
quietly do EITC_Calculator.do


** Now merge in simulated EITC amount (average for cell defined
** in terms of YearOfTaxCalc, tfipsst, Married, KidsForSimulated
gen YearOfTaxCalc = rhcalyr-1
gen KidsForSimulated = DepCount
replace KidsForSimulated = 3 if KidsForSimulated>3
merge m:1 YearOfTaxCalc tfipsst Married KidsForSimulated using SimulatedEITC
drop if _merge==2
drop _merge
gen taxsim_eic_thou = (ts_eic_fed + ts_eic_state)/1000

rename Married MidGenPar_Married
rename Employed MidGenPar_Employed
rename HSGrad MidGenPar_HSGrad
rename SomeCollege MidGenPar_SomeCollege
rename CollegeGrad MidGenPar_CollegeGrad
rename White MidGenPar_White
rename PotentialDep MidGenPar_PotentialDep


gen SingleMom = (Female==1 & MidGenPar_Married==0)
gen SingleNoCollegeMom = (Female==1 & MidGenPar_Married==0 & MidGenPar_SomeCollege==0 & MidGenPar_CollegeGrad==0)
gen temp_college = (MidGenPar_SomeCollege==1 | MidGenPar_CollegeGrad==1)


sort ssuid srefmon
by ssuid srefmon: egen N_SingleMom=total(SingleMom)
by ssuid srefmon: egen N_SingleNoCollegeMom = total(SingleNoCollegeMom)
by ssuid srefmon: egen N_ParsWithCollege=total(temp_college)
by ssuid srefmon: egen N_CollegeGradPars=total(MidGenPar_CollegeGrad)
by ssuid srefmon: egen N_ParsEmployed=total(MidGenPar_Employed)
by ssuid srefmon: egen N_ParsPotentialDep=total(MidGenPar_PotentialDep)

gen AnyParsPotentialDeps=(N_ParsPotentialDep>0)
gen AnySingleMom = (N_SingleMom>0)
gen AnySingleNoCollegeMom=(N_SingleNoCollegeMom>0)
gen NoParsWithCollege = (N_ParsWithCollege==0)
gen NoCollegeGradPars = (N_CollegeGradPars==0)
gen AnyParsEmployed = (N_ParsEmployed>0)
drop temp_college

gen RefPerson=(errp==1 | errp==2)
sort ssuid srefmon
by ssuid  srefmon: egen N_RefPerson=total(RefPerson)
gen MidGenHasRefPerson=(N_RefPerson>0)

merge m:1 ssuid epppnum using WellBeingVars1996_w3 
keep if _merge==3
drop _merge



*******************************************************************************
** Merge in data on receipt of childcare from a grandparent 

merge m:1 ssuid epppnum using ChildCareVars_1996_w4
drop if _merge==2
drop _merge 

sort ssuid srefmon
by ssuid srefmon: egen N_GetCCFromGP=total(GetCCFromGP)
gen AnyParsGetCCFromGP = (N_GetCCFromGP>0)


gen panel=1996
save OrganizedMidGenParentData1996_w3, replace


** Extract information at household-month level about MAXIMUM amount of EITC eligibility
** for which any mid-gen parent is available.  The goal is to make this info ready 
** for merging with grandparents who live in those same households.

keep panel swave ssuid srefmon r_MaxEITC_thou r_ref_MaxEITC currentyear_r_MaxEITC_thou taxsim_eic_thou xocah AnySingleMom AnySingleNoCollegeMom NoParsWithCollege NoCollegeGradPars AnyParsEmployed MidGenHasRefPerson AnyParsPotentialDeps r_MaxFedEITC_thou ts_eic_fed AnyParsGetCCFromGP
sort ssuid srefmon
by ssuid srefmon: egen r_MaxEITC_MidGenMax=max(r_MaxEITC_thou)
by ssuid srefmon: egen r_ref_MaxEITC_MidGenMax=max(r_ref_MaxEITC)
by ssuid srefmon: egen currentyear_r_MaxEITC_MidGenMax=max(currentyear_r_MaxEITC_thou)
by ssuid srefmon: egen taxsim_eic_MidGenMax=max(taxsim_eic_thou)
by ssuid srefmon: egen r_MaxFedEITC_MidGenMax=max(r_MaxFedEITC_thou)
by ssuid srefmon: egen taxsim_eic_Fed_MidGenMax=max(ts_eic_fed)

** Only keep observations of individuals whose EITC eligibility is equal to the max EITC
** eligibility of anyone in the middle generation
keep if r_MaxEITC_MidGenMax == r_MaxEITC_thou

** For later construction of a summary stats table, distinguish between cases of 1 EITC-eligible
** child and 2+ EITC-eligible children of the middle-generation parent who is eligible for the largest
** EITC amount
gen xocah_1or2plus=.
replace xocah_1or2plus = 1 if xocah==1
replace xocah_1or2plus = 2 if xocah>1 

drop xocah r_MaxEITC_thou r_ref_MaxEITC currentyear_r_MaxEITC_thou taxsim_eic_thou ts_eic_fed
duplicates drop

duplicates report ssuid srefmon
save MaxMidGenEITC1996_w3, replace
clear


** Another way to count EITC resources in the middle generation is to sum up amounts received
** by different mid-gen parents.  You don't want to double-count EITC eligibility of a husband
** and wife, but you could assume that all mothers are separate filers and sum up all EITC
** amounts of the different mid-gen mothers.  
use OrganizedMidGenParentData1996_w3

keep panel swave ssuid srefmon r_MaxEITC_thou r_ref_MaxEITC currentyear_r_MaxEITC_thou currentyear_r_ref_MaxEITC taxsim_eic_thou PotentialMidGenMom r_MaxFedEITC_thou ts_eic_fed
keep if PotentialMidGenMom==1
duplicates report ssuid srefmon
sort ssuid srefmon
by ssuid srefmon: egen Sum_MaxEITC_MidGenMoms = sum(r_MaxEITC_thou)
by ssuid srefmon: egen Sum_ref_MaxEITC_MidGenMoms = sum(r_ref_MaxEITC)
by ssuid srefmon: egen Sum_cy_MaxEITC_MidGenMoms = sum(currentyear_r_MaxEITC_thou)
by ssuid srefmon: egen Sum_cy_ref_MaxEITC_MidGenMoms = sum(currentyear_r_ref_MaxEITC)
by ssuid srefmon: egen Sum_taxsim_eic_MidGenMoms=sum(taxsim_eic_thou)
by ssuid srefmon: egen Sum_MaxFedEITC_MidGenMoms = sum(r_MaxFedEITC_thou)
by ssuid srefmon: egen Sum_taxsim_eic_Fed_MidGenMoms=sum(ts_eic_fed)

keep panel swave ssuid srefmon Sum_MaxEITC_MidGenMoms Sum_ref_MaxEITC_MidGenMoms Sum_cy_MaxEITC_MidGenMoms Sum_cy_ref_MaxEITC_MidGenMoms Sum_taxsim_eic_MidGenMoms Sum_MaxFedEITC_MidGenMoms Sum_taxsim_eic_Fed_MidGenMoms
duplicates drop
save SumMidGenEITC_1996_w3, replace
clear



***********
** Wave 6
***********

use MidGenParentData1996_w6
** Merge in count of how many people in household identified this person as his/her mother
merge 1:1 ssuid epppnum srefmon using KidCount1996_w6
assert _merge~=1
keep if _merge==3
drop _merge


** EXPERIMENTING: Try a version where you DON'T subtract one from year
** before doing EITC calculations
gen EITC_calc_year = rhcalyr
gen xocah = DepCount
quietly do EITC_Calculator.do
drop MaxFedEITC
drop MaxStateEITC
drop StateEITC_NonRefund
rename r_MaxEITC currentyear_r_MaxEITC
rename r_MaxEITC_thou currentyear_r_MaxEITC_thou
rename r_MaxFedEITC_thou currentyear_r_MaxFedEITC_thou
rename r_MaxStateEITC_thou  currentyear_r_MaxStateEITC_thou
rename r_ref_MaxEITC  currentyear_r_ref_MaxEITC
drop real
drop EITC_calc_year xocah

gen EITC_calc_year = rhcalyr-1
gen xocah = DepCount
quietly do EITC_Calculator.do

gen YearOfTaxCalc = rhcalyr-1
gen KidsForSimulated = DepCount
replace KidsForSimulated = 3 if KidsForSimulated>3 
merge m:1 YearOfTaxCalc tfipsst Married KidsForSimulated using SimulatedEITC
drop if _merge==2
drop _merge
gen taxsim_eic_thou = (ts_eic_fed + ts_eic_state)/1000

rename Married MidGenPar_Married
rename Employed MidGenPar_Employed
rename HSGrad MidGenPar_HSGrad
rename SomeCollege MidGenPar_SomeCollege
rename CollegeGrad MidGenPar_CollegeGrad
rename White MidGenPar_White
rename PotentialDep MidGenPar_PotentialDep


gen SingleMom = (Female==1 & MidGenPar_Married==0)
gen SingleNoCollegeMom = (Female==1 & MidGenPar_Married==0 & MidGenPar_SomeCollege==0 & MidGenPar_CollegeGrad==0)
gen temp_college = (MidGenPar_SomeCollege==1 | MidGenPar_CollegeGrad==1)


sort ssuid srefmon
by ssuid srefmon: egen N_SingleMom=total(SingleMom)
by ssuid srefmon: egen N_SingleNoCollegeMom = total(SingleNoCollegeMom)
by ssuid srefmon: egen N_ParsWithCollege=total(temp_college)
by ssuid srefmon: egen N_CollegeGradPars=total(MidGenPar_CollegeGrad)
by ssuid srefmon: egen N_ParsEmployed=total(MidGenPar_Employed)
by ssuid srefmon: egen N_ParsPotentialDep=total(MidGenPar_PotentialDep)

gen AnyParsPotentialDeps=(N_ParsPotentialDep>0)
gen AnySingleMom = (N_SingleMom>0)
gen AnySingleNoCollegeMom=(N_SingleNoCollegeMom>0)
gen NoParsWithCollege = (N_ParsWithCollege==0)
gen NoCollegeGradPars = (N_CollegeGradPars==0)
gen AnyParsEmployed = (N_ParsEmployed>0)
drop temp_college

gen RefPerson=(errp==1 | errp==2)
sort ssuid  srefmon
by ssuid  srefmon: egen N_RefPerson=total(RefPerson)
gen MidGenHasRefPerson=(N_RefPerson>0)

merge m:1 ssuid epppnum using WellBeingVars1996_w6 
keep if _merge==3
drop _merge

gen panel=1996
save OrganizedMidGenParentData1996_w6, replace


** Extract information at household-month level about MAXIMUM amount of EITC eligibility
** for which any mid-gen parent is available.  The goal is to make this info ready 
** for merging with grandparents who live in those same households.

keep panel swave ssuid srefmon r_MaxEITC_thou r_ref_MaxEITC currentyear_r_MaxEITC_thou taxsim_eic_thou xocah AnySingleMom AnySingleNoCollegeMom NoParsWithCollege NoCollegeGradPars AnyParsEmployed MidGenHasRefPerson AnyParsPotentialDeps r_MaxFedEITC_thou ts_eic_fed
sort ssuid srefmon
by ssuid srefmon: egen r_MaxEITC_MidGenMax=max(r_MaxEITC_thou)
by ssuid srefmon: egen r_ref_MaxEITC_MidGenMax=max(r_ref_MaxEITC)
by ssuid srefmon: egen currentyear_r_MaxEITC_MidGenMax=max(currentyear_r_MaxEITC_thou)
by ssuid srefmon: egen taxsim_eic_MidGenMax=max(taxsim_eic_thou)
by ssuid srefmon: egen r_MaxFedEITC_MidGenMax=max(r_MaxFedEITC_thou)
by ssuid srefmon: egen taxsim_eic_Fed_MidGenMax=max(ts_eic_fed)

** Only keep observations of individuals whose EITC eligibility is equal to the max EITC
** eligibility of anyone in the middle generation
keep if r_MaxEITC_MidGenMax == r_MaxEITC_thou

** For later construction of a summary stats table, distinguish between cases of 1 EITC-eligible
** child and 2+ EITC-eligible children of the middle-generation parent who is eligible for the largest
** EITC amount
gen xocah_1or2plus=.
replace xocah_1or2plus = 1 if xocah==1
replace xocah_1or2plus = 2 if xocah>1 

drop xocah r_MaxEITC_thou r_ref_MaxEITC currentyear_r_MaxEITC_thou taxsim_eic_thou ts_eic_fed
duplicates drop

duplicates report ssuid srefmon
save MaxMidGenEITC1996_w6, replace
clear


** Another way to count EITC resources in the middle generation is to sum up amounts received
** by different mid-gen parents.  You don't want to double-count EITC eligibility of a husband
** and wife, but you could assume that all mothers are separate filers and sum up all EITC
** amounts of the different mid-gen mothers.  
use OrganizedMidGenParentData1996_w6

keep panel swave ssuid srefmon r_MaxEITC_thou r_ref_MaxEITC currentyear_r_MaxEITC_thou currentyear_r_ref_MaxEITC taxsim_eic_thou PotentialMidGenMom r_MaxFedEITC_thou ts_eic_fed
keep if PotentialMidGenMom==1
duplicates report ssuid srefmon
sort ssuid srefmon
by ssuid srefmon: egen Sum_MaxEITC_MidGenMoms = sum(r_MaxEITC_thou)
by ssuid srefmon: egen Sum_ref_MaxEITC_MidGenMoms = sum(r_ref_MaxEITC)
by ssuid srefmon: egen Sum_cy_MaxEITC_MidGenMoms = sum(currentyear_r_MaxEITC_thou)
by ssuid srefmon: egen Sum_cy_ref_MaxEITC_MidGenMoms = sum(currentyear_r_ref_MaxEITC)
by ssuid srefmon: egen Sum_taxsim_eic_MidGenMoms = sum(taxsim_eic_thou)
by ssuid srefmon: egen Sum_MaxFedEITC_MidGenMoms = sum(r_MaxFedEITC_thou)
by ssuid srefmon: egen Sum_taxsim_eic_Fed_MidGenMoms = sum(ts_eic_fed)

keep panel swave ssuid srefmon Sum_MaxEITC_MidGenMoms Sum_ref_MaxEITC_MidGenMoms Sum_cy_MaxEITC_MidGenMoms Sum_cy_ref_MaxEITC_MidGenMoms Sum_taxsim_eic_MidGenMoms Sum_MaxFedEITC_MidGenMoms Sum_taxsim_eic_Fed_MidGenMoms
duplicates drop
save SumMidGenEITC_1996_w6, replace
clear



***********
** Wave 9
***********

use MidGenParentData1996_w9
** Merge in count of how many people in household identified this person as his/her mother
merge 1:1 ssuid epppnum srefmon using KidCount1996_w9
assert _merge~=1
keep if _merge==3
drop _merge


** EXPERIMENTING: Try a version where you DON'T subtract one from year
** before doing EITC calculations
gen EITC_calc_year = rhcalyr
gen xocah = DepCount
quietly do EITC_Calculator.do
drop MaxFedEITC
drop MaxStateEITC
drop StateEITC_NonRefund
rename r_MaxEITC currentyear_r_MaxEITC
rename r_MaxEITC_thou currentyear_r_MaxEITC_thou
rename r_MaxFedEITC_thou currentyear_r_MaxFedEITC_thou
rename r_MaxStateEITC_thou  currentyear_r_MaxStateEITC_thou
rename r_ref_MaxEITC  currentyear_r_ref_MaxEITC
drop real
drop EITC_calc_year xocah

gen EITC_calc_year = rhcalyr-1
gen xocah = DepCount
quietly do EITC_Calculator.do

gen YearOfTaxCalc = rhcalyr-1
gen KidsForSimulated = DepCount
replace KidsForSimulated = 3 if KidsForSimulated>3
merge m:1 YearOfTaxCalc tfipsst Married KidsForSimulated using SimulatedEITC
drop if _merge==2
drop _merge
gen taxsim_eic_thou = (ts_eic_fed + ts_eic_state)/1000

rename Married MidGenPar_Married
rename Employed MidGenPar_Employed
rename HSGrad MidGenPar_HSGrad
rename SomeCollege MidGenPar_SomeCollege
rename CollegeGrad MidGenPar_CollegeGrad
rename White MidGenPar_White
rename PotentialDep MidGenPar_PotentialDep


gen SingleMom = (Female==1 & MidGenPar_Married==0)
gen SingleNoCollegeMom = (Female==1 & MidGenPar_Married==0 & MidGenPar_SomeCollege==0 & MidGenPar_CollegeGrad==0)
gen temp_college = (MidGenPar_SomeCollege==1 | MidGenPar_CollegeGrad==1)


sort ssuid srefmon
by ssuid srefmon: egen N_SingleMom=total(SingleMom)
by ssuid srefmon: egen N_SingleNoCollegeMom = total(SingleNoCollegeMom)
by ssuid srefmon: egen N_ParsWithCollege=total(temp_college)
by ssuid srefmon: egen N_CollegeGradPars=total(MidGenPar_CollegeGrad)
by ssuid srefmon: egen N_ParsEmployed=total(MidGenPar_Employed)
by ssuid srefmon: egen N_ParsPotentialDep=total(MidGenPar_PotentialDep)

gen AnyParsPotentialDeps=(N_ParsPotentialDep>0)
gen AnySingleMom = (N_SingleMom>0)
gen AnySingleNoCollegeMom=(N_SingleNoCollegeMom>0)
gen NoParsWithCollege = (N_ParsWithCollege==0)
gen NoCollegeGradPars = (N_CollegeGradPars==0)
gen AnyParsEmployed = (N_ParsEmployed>0)
drop temp_college

gen RefPerson=(errp==1 | errp==2)
sort ssuid srefmon
by ssuid srefmon: egen N_RefPerson=total(RefPerson)
gen MidGenHasRefPerson=(N_RefPerson>0)

merge m:1 ssuid epppnum using WellBeingVars1996_w9 
keep if _merge==3
drop _merge


*******************************************************************************
** Merge in data on receipt of childcare from a grandparent 

merge m:1 ssuid epppnum using ChildCareVars_1996_w10
drop if _merge==2
drop _merge 

sort ssuid srefmon
by ssuid srefmon: egen N_GetCCFromGP=total(GetCCFromGP)
gen AnyParsGetCCFromGP = (N_GetCCFromGP>0)


gen panel=1996
save OrganizedMidGenParentData1996_w9, replace


** Extract information at household-month level about MAXIMUM amount of EITC eligibility
** for which any mid-gen parent is available.  The goal is to make this info ready 
** for merging with grandparents who live in those same households.

keep panel swave ssuid srefmon r_MaxEITC_thou r_ref_MaxEITC currentyear_r_MaxEITC_thou taxsim_eic_thou xocah AnySingleMom AnySingleNoCollegeMom NoParsWithCollege NoCollegeGradPars AnyParsEmployed MidGenHasRefPerson AnyParsPotentialDeps r_MaxFedEITC_thou ts_eic_fed AnyParsGetCCFromGP
sort ssuid srefmon
by ssuid srefmon: egen r_MaxEITC_MidGenMax=max(r_MaxEITC_thou)
by ssuid srefmon: egen r_ref_MaxEITC_MidGenMax=max(r_ref_MaxEITC)
by ssuid srefmon: egen currentyear_r_MaxEITC_MidGenMax=max(currentyear_r_MaxEITC_thou)
by ssuid srefmon: egen taxsim_eic_MidGenMax=max(taxsim_eic_thou)
by ssuid srefmon: egen r_MaxFedEITC_MidGenMax=max(r_MaxFedEITC_thou)
by ssuid srefmon: egen taxsim_eic_Fed_MidGenMax=max(ts_eic_fed)

** Only keep observations of individuals whose EITC eligibility is equal to the max EITC
** eligibility of anyone in the middle generation
keep if r_MaxEITC_MidGenMax == r_MaxEITC_thou

** For later construction of a summary stats table, distinguish between cases of 1 EITC-eligible
** child and 2+ EITC-eligible children of the middle-generation parent who is eligible for the largest
** EITC amount
gen xocah_1or2plus=.
replace xocah_1or2plus = 1 if xocah==1
replace xocah_1or2plus = 2 if xocah>1 

drop xocah r_MaxEITC_thou r_ref_MaxEITC currentyear_r_MaxEITC_thou taxsim_eic_thou ts_eic_fed
duplicates drop

duplicates report ssuid srefmon
save MaxMidGenEITC1996_w9, replace
clear



** Another way to count EITC resources in the middle generation is to sum up amounts received
** by different mid-gen parents.  You don't want to double-count EITC eligibility of a husband
** and wife, but you could assume that all mothers are separate filers and sum up all EITC
** amounts of the different mid-gen mothers.  
use OrganizedMidGenParentData1996_w9

keep panel swave ssuid srefmon r_MaxEITC_thou r_ref_MaxEITC currentyear_r_MaxEITC_thou currentyear_r_ref_MaxEITC taxsim_eic_thou PotentialMidGenMom r_MaxFedEITC_thou ts_eic_fed
keep if PotentialMidGenMom==1
duplicates report ssuid srefmon
sort ssuid srefmon
by ssuid srefmon: egen Sum_MaxEITC_MidGenMoms = sum(r_MaxEITC_thou)
by ssuid srefmon: egen Sum_ref_MaxEITC_MidGenMoms = sum(r_ref_MaxEITC)
by ssuid srefmon: egen Sum_cy_MaxEITC_MidGenMoms = sum(currentyear_r_MaxEITC_thou)
by ssuid srefmon: egen Sum_cy_ref_MaxEITC_MidGenMoms = sum(currentyear_r_ref_MaxEITC)
by ssuid srefmon: egen Sum_taxsim_eic_MidGenMoms = sum(taxsim_eic_thou)
by ssuid srefmon: egen Sum_MaxFedEITC_MidGenMoms = sum(r_MaxFedEITC_thou)
by ssuid srefmon: egen Sum_taxsim_eic_Fed_MidGenMoms = sum(ts_eic_fed)

keep panel swave ssuid srefmon Sum_MaxEITC_MidGenMoms Sum_ref_MaxEITC_MidGenMoms Sum_cy_MaxEITC_MidGenMoms Sum_cy_ref_MaxEITC_MidGenMoms Sum_taxsim_eic_MidGenMoms Sum_MaxFedEITC_MidGenMoms Sum_taxsim_eic_Fed_MidGenMoms
duplicates drop
save SumMidGenEITC_1996_w9, replace
clear



***********
** Wave 12
***********

use  MidGenParentData1996_w12
** Merge in count of how many people in household identified this person as his/her mother
merge 1:1 ssuid epppnum srefmon using KidCount1996_w12
assert _merge~=1
keep if _merge==3
drop _merge


** EXPERIMENTING: Try a version where you DON'T subtract one from year
** before doing EITC calculations
gen EITC_calc_year = rhcalyr
gen xocah = DepCount
quietly do EITC_Calculator.do
drop MaxFedEITC
drop MaxStateEITC
drop StateEITC_NonRefund
rename r_MaxEITC currentyear_r_MaxEITC
rename r_MaxEITC_thou currentyear_r_MaxEITC_thou
rename r_MaxFedEITC_thou currentyear_r_MaxFedEITC_thou
rename r_MaxStateEITC_thou  currentyear_r_MaxStateEITC_thou
rename r_ref_MaxEITC  currentyear_r_ref_MaxEITC
drop real
drop EITC_calc_year xocah

gen EITC_calc_year = rhcalyr-1
gen xocah = DepCount
quietly do EITC_Calculator.do

gen YearOfTaxCalc = rhcalyr-1
gen KidsForSimulated = DepCount
replace KidsForSimulated = 3 if KidsForSimulated>3
merge m:1 YearOfTaxCalc tfipsst Married KidsForSimulated using SimulatedEITC
drop if _merge==2
drop _merge
gen taxsim_eic_thou = (ts_eic_fed + ts_eic_state)/1000


rename Married MidGenPar_Married
rename Employed MidGenPar_Employed
rename HSGrad MidGenPar_HSGrad
rename SomeCollege MidGenPar_SomeCollege
rename CollegeGrad MidGenPar_CollegeGrad
rename White MidGenPar_White
rename PotentialDep MidGenPar_PotentialDep


gen SingleMom = (Female==1 & MidGenPar_Married==0)
gen SingleNoCollegeMom = (Female==1 & MidGenPar_Married==0 & MidGenPar_SomeCollege==0 & MidGenPar_CollegeGrad==0)
gen temp_college = (MidGenPar_SomeCollege==1 | MidGenPar_CollegeGrad==1)


sort ssuid srefmon
by ssuid srefmon: egen N_SingleMom=total(SingleMom)
by ssuid srefmon: egen N_SingleNoCollegeMom = total(SingleNoCollegeMom)
by ssuid srefmon: egen N_ParsWithCollege=total(temp_college)
by ssuid srefmon: egen N_CollegeGradPars=total(MidGenPar_CollegeGrad)
by ssuid srefmon: egen N_ParsEmployed=total(MidGenPar_Employed)
by ssuid srefmon: egen N_ParsPotentialDep=total(MidGenPar_PotentialDep)

gen AnyParsPotentialDeps=(N_ParsPotentialDep>0)
gen AnySingleMom = (N_SingleMom>0)
gen AnySingleNoCollegeMom=(N_SingleNoCollegeMom>0)
gen NoParsWithCollege = (N_ParsWithCollege==0)
gen NoCollegeGradPars = (N_CollegeGradPars==0)
gen AnyParsEmployed = (N_ParsEmployed>0)
drop temp_college

gen RefPerson=(errp==1 | errp==2)
sort ssuid srefmon
by ssuid srefmon: egen N_RefPerson=total(RefPerson)
gen MidGenHasRefPerson=(N_RefPerson>0)

merge m:1 ssuid epppnum using WellBeingVars1996_w12 
keep if _merge==3
drop _merge

gen panel=1996
save OrganizedMidGenParentData1996_w12, replace


** Extract information at household-month level about MAXIMUM amount of EITC eligibility
** for which any mid-gen parent is available.  The goal is to make this info ready 
** for merging with grandparents who live in those same households.

keep panel swave ssuid srefmon r_MaxEITC_thou r_ref_MaxEITC currentyear_r_MaxEITC_thou taxsim_eic_thou xocah AnySingleMom AnySingleNoCollegeMom NoParsWithCollege NoCollegeGradPars AnyParsEmployed MidGenHasRefPerson AnyParsPotentialDeps r_MaxFedEITC_thou ts_eic_fed
sort ssuid srefmon
by ssuid srefmon: egen r_MaxEITC_MidGenMax=max(r_MaxEITC_thou)
by ssuid srefmon: egen r_ref_MaxEITC_MidGenMax=max(r_ref_MaxEITC)
by ssuid srefmon: egen currentyear_r_MaxEITC_MidGenMax=max(currentyear_r_MaxEITC_thou)
by ssuid srefmon: egen taxsim_eic_MidGenMax=max(taxsim_eic_thou)
by ssuid srefmon: egen r_MaxFedEITC_MidGenMax=max(r_MaxFedEITC_thou)
by ssuid srefmon: egen taxsim_eic_Fed_MidGenMax=max(ts_eic_fed)

** Only keep observations of individuals whose EITC eligibility is equal to the max EITC
** eligibility of anyone in the middle generation
keep if r_MaxEITC_MidGenMax == r_MaxEITC_thou

** For later construction of a summary stats table, distinguish between cases of 1 EITC-eligible
** child and 2+ EITC-eligible children of the middle-generation parent who is eligible for the largest
** EITC amount
gen xocah_1or2plus=.
replace xocah_1or2plus = 1 if xocah==1
replace xocah_1or2plus = 2 if xocah>1 

drop xocah r_MaxEITC_thou r_ref_MaxEITC currentyear_r_MaxEITC_thou taxsim_eic_thou ts_eic_fed
duplicates drop

duplicates report ssuid srefmon
save MaxMidGenEITC1996_w12, replace
clear



** Another way to count EITC resources in the middle generation is to sum up amounts received
** by different mid-gen parents.  You don't want to double-count EITC eligibility of a husband
** and wife, but you could assume that all mothers are separate filers and sum up all EITC
** amounts of the different mid-gen mothers.  
use OrganizedMidGenParentData1996_w12

keep panel swave ssuid srefmon r_MaxEITC_thou r_ref_MaxEITC currentyear_r_MaxEITC_thou currentyear_r_ref_MaxEITC taxsim_eic_thou PotentialMidGenMom r_MaxFedEITC_thou ts_eic_fed
keep if PotentialMidGenMom==1
duplicates report ssuid srefmon
sort ssuid srefmon
by ssuid srefmon: egen Sum_MaxEITC_MidGenMoms = sum(r_MaxEITC_thou)
by ssuid srefmon: egen Sum_ref_MaxEITC_MidGenMoms = sum(r_ref_MaxEITC)
by ssuid srefmon: egen Sum_cy_MaxEITC_MidGenMoms = sum(currentyear_r_MaxEITC_thou)
by ssuid srefmon: egen Sum_cy_ref_MaxEITC_MidGenMoms = sum(currentyear_r_ref_MaxEITC)
by ssuid srefmon: egen Sum_taxsim_eic_MidGenMoms = sum(taxsim_eic_thou)
by ssuid srefmon: egen Sum_MaxFedEITC_MidGenMoms = sum(r_MaxFedEITC_thou)
by ssuid srefmon: egen Sum_taxsim_eic_Fed_MidGenMoms = sum(ts_eic_fed)

keep panel swave ssuid srefmon Sum_MaxEITC_MidGenMoms Sum_ref_MaxEITC_MidGenMoms Sum_cy_MaxEITC_MidGenMoms Sum_cy_ref_MaxEITC_MidGenMoms Sum_taxsim_eic_MidGenMoms Sum_MaxFedEITC_MidGenMoms Sum_taxsim_eic_Fed_MidGenMoms
duplicates drop
save SumMidGenEITC_1996_w12, replace
clear





******************
** 2001
******************

**********
** Wave 3
**********

use  MidGenParentData2001_w3
** Merge in count of how many people in household identified this person as his/her mother
merge 1:1 ssuid epppnum srefmon using KidCount2001_w3
assert _merge~=1
keep if _merge==3
drop _merge


** EXPERIMENTING: Try a version where you DON'T subtract one from year
** before doing EITC calculations
gen EITC_calc_year = rhcalyr
gen xocah = DepCount
quietly do EITC_Calculator.do
drop MaxFedEITC
drop MaxStateEITC
drop StateEITC_NonRefund
rename r_MaxEITC currentyear_r_MaxEITC
rename r_MaxEITC_thou currentyear_r_MaxEITC_thou
rename r_MaxFedEITC_thou currentyear_r_MaxFedEITC_thou
rename r_MaxStateEITC_thou  currentyear_r_MaxStateEITC_thou
rename r_ref_MaxEITC  currentyear_r_ref_MaxEITC
drop real
drop EITC_calc_year xocah

** But primary EITC calculation uses tax law of one year ago (consistent with
** refund amount family likely has on hand over time period when well-being 
** variables are being measured
gen EITC_calc_year = rhcalyr-1
gen xocah = DepCount
quietly do EITC_Calculator.do

gen YearOfTaxCalc = rhcalyr-1
gen KidsForSimulated = DepCount
replace KidsForSimulated = 3 if KidsForSimulated>3
merge m:1 YearOfTaxCalc tfipsst Married KidsForSimulated using SimulatedEITC
drop if _merge==2
drop _merge
gen taxsim_eic_thou = (ts_eic_fed + ts_eic_state)/1000

rename Married MidGenPar_Married
rename Employed MidGenPar_Employed
rename HSGrad MidGenPar_HSGrad
rename SomeCollege MidGenPar_SomeCollege
rename CollegeGrad MidGenPar_CollegeGrad
rename White MidGenPar_White
rename PotentialDep MidGenPar_PotentialDep


gen SingleMom = (Female==1 & MidGenPar_Married==0)
gen SingleNoCollegeMom = (Female==1 & MidGenPar_Married==0 & MidGenPar_SomeCollege==0 & MidGenPar_CollegeGrad==0)
gen temp_college = (MidGenPar_SomeCollege==1 | MidGenPar_CollegeGrad==1)

sort ssuid srefmon
by ssuid srefmon: egen N_SingleMom=total(SingleMom)
by ssuid srefmon: egen N_SingleNoCollegeMom = total(SingleNoCollegeMom)
by ssuid srefmon: egen N_ParsWithCollege=total(temp_college)
by ssuid srefmon: egen N_CollegeGradPars=total(MidGenPar_CollegeGrad)
by ssuid srefmon: egen N_ParsEmployed=total(MidGenPar_Employed)
by ssuid srefmon: egen N_ParsPotentialDep=total(MidGenPar_PotentialDep)

gen AnyParsPotentialDeps=(N_ParsPotentialDep>0)
gen AnySingleMom = (N_SingleMom>0)
gen AnySingleNoCollegeMom=(N_SingleNoCollegeMom>0)
gen NoParsWithCollege = (N_ParsWithCollege==0)
gen NoCollegeGradPars = (N_CollegeGradPars==0)
gen AnyParsEmployed = (N_ParsEmployed>0)
drop temp_college

gen RefPerson=(errp==1 | errp==2)
sort ssuid srefmon
by ssuid srefmon: egen N_RefPerson=total(RefPerson)
gen MidGenHasRefPerson=(N_RefPerson>0)

merge m:1 ssuid epppnum using WellBeingVars2001_w3 
keep if _merge==3
drop _merge

*



*******************************************************************************
** Merge in data on receipt of childcare from a grandparent 

merge m:1 ssuid epppnum using ChildCareVars_2001_w4
drop if _merge==2
drop _merge 

sort ssuid srefmon
by ssuid srefmon: egen N_GetCCFromGP=total(GetCCFromGP)
gen AnyParsGetCCFromGP = (N_GetCCFromGP>0)


gen panel=2001
save OrganizedMidGenParentData2001_w3, replace


** Extract information at household-month level about MAXIMUM amount of EITC eligibility
** for which any mid-gen parent is available.  The goal is to make this info ready 
** for merging with grandparents who live in those same households.

keep panel swave ssuid srefmon r_MaxEITC_thou r_ref_MaxEITC currentyear_r_MaxEITC_thou taxsim_eic_thou xocah AnySingleMom AnySingleNoCollegeMom NoParsWithCollege NoCollegeGradPars AnyParsEmployed MidGenHasRefPerson AnyParsPotentialDeps r_MaxFedEITC_thou ts_eic_fed AnyParsGetCCFromGP
sort ssuid srefmon
by ssuid srefmon: egen r_MaxEITC_MidGenMax=max(r_MaxEITC_thou)
by ssuid srefmon: egen r_ref_MaxEITC_MidGenMax=max(r_ref_MaxEITC)
by ssuid srefmon: egen currentyear_r_MaxEITC_MidGenMax=max(currentyear_r_MaxEITC_thou)
by ssuid srefmon: egen taxsim_eic_MidGenMax=max(taxsim_eic_thou)
by ssuid srefmon: egen r_MaxFedEITC_MidGenMax=max(r_MaxFedEITC_thou)
by ssuid srefmon: egen taxsim_eic_Fed_MidGenMax=max(ts_eic_fed)

** Only keep observations of individuals whose EITC eligibility is equal to the max EITC
** eligibility of anyone in the middle generation
keep if r_MaxEITC_MidGenMax == r_MaxEITC_thou

** For later construction of a summary stats table, distinguish between cases of 1 EITC-eligible
** child and 2+ EITC-eligible children of the middle-generation parent who is eligible for the largest
** EITC amount
gen xocah_1or2plus=.
replace xocah_1or2plus = 1 if xocah==1
replace xocah_1or2plus = 2 if xocah>1 

drop xocah r_MaxEITC_thou r_ref_MaxEITC currentyear_r_MaxEITC_thou taxsim_eic_thou ts_eic_fed
duplicates drop

duplicates report ssuid srefmon

save MaxMidGenEITC2001_w3, replace
clear




** Another way to count EITC resources in the middle generation is to sum up amounts received
** by different mid-gen parents.  You don't want to double-count EITC eligibility of a husband
** and wife, but you could assume that all mothers are separate filers and sum up all EITC
** amounts of the different mid-gen mothers.  
use OrganizedMidGenParentData2001_w3

keep panel swave ssuid srefmon r_MaxEITC_thou r_ref_MaxEITC currentyear_r_MaxEITC_thou currentyear_r_ref_MaxEITC taxsim_eic_thou PotentialMidGenMom r_MaxFedEITC_thou ts_eic_fed
keep if PotentialMidGenMom==1
duplicates report ssuid srefmon
sort ssuid srefmon
by ssuid srefmon: egen Sum_MaxEITC_MidGenMoms = sum(r_MaxEITC_thou)
by ssuid srefmon: egen Sum_ref_MaxEITC_MidGenMoms = sum(r_ref_MaxEITC)
by ssuid srefmon: egen Sum_cy_MaxEITC_MidGenMoms = sum(currentyear_r_MaxEITC_thou)
by ssuid srefmon: egen Sum_cy_ref_MaxEITC_MidGenMoms = sum(currentyear_r_ref_MaxEITC)
by ssuid srefmon: egen Sum_taxsim_eic_MidGenMoms = sum(taxsim_eic_thou)
by ssuid srefmon: egen Sum_MaxFedEITC_MidGenMoms = sum(r_MaxFedEITC_thou)
by ssuid srefmon: egen Sum_taxsim_eic_Fed_MidGenMoms = sum(ts_eic_fed)

keep panel swave ssuid srefmon Sum_MaxEITC_MidGenMoms Sum_ref_MaxEITC_MidGenMoms Sum_cy_MaxEITC_MidGenMoms Sum_cy_ref_MaxEITC_MidGenMoms Sum_taxsim_eic_MidGenMoms Sum_MaxFedEITC_MidGenMoms Sum_taxsim_eic_Fed_MidGenMoms
duplicates drop
save SumMidGenEITC_2001_w3, replace
clear


**********
** Wave 6
**********

use  MidGenParentData2001_w6
** Merge in count of how many people in household identified this person as his/her mother
merge 1:1 ssuid epppnum srefmon using KidCount2001_w6
assert _merge~=1
keep if _merge==3
drop _merge


** EXPERIMENTING: Try a version where you DON'T subtract one from year
** before doing EITC calculations
gen EITC_calc_year = rhcalyr
gen xocah = DepCount
quietly do EITC_Calculator.do
drop MaxFedEITC
drop MaxStateEITC
drop StateEITC_NonRefund
rename r_MaxEITC currentyear_r_MaxEITC
rename r_MaxEITC_thou currentyear_r_MaxEITC_thou
rename r_MaxFedEITC_thou currentyear_r_MaxFedEITC_thou
rename r_MaxStateEITC_thou  currentyear_r_MaxStateEITC_thou
rename r_ref_MaxEITC  currentyear_r_ref_MaxEITC
drop real
drop EITC_calc_year xocah

gen EITC_calc_year = rhcalyr-1
gen xocah = DepCount
quietly do EITC_Calculator.do

gen YearOfTaxCalc = rhcalyr-1
gen KidsForSimulated = DepCount
replace KidsForSimulated = 3 if KidsForSimulated>3
merge m:1 YearOfTaxCalc tfipsst Married KidsForSimulated using SimulatedEITC
drop if _merge==2
drop _merge
gen taxsim_eic_thou = (ts_eic_fed + ts_eic_state)/1000

rename Married MidGenPar_Married
rename Employed MidGenPar_Employed
rename HSGrad MidGenPar_HSGrad
rename SomeCollege MidGenPar_SomeCollege
rename CollegeGrad MidGenPar_CollegeGrad
rename White MidGenPar_White
rename PotentialDep MidGenPar_PotentialDep

gen SingleMom = (Female==1 & MidGenPar_Married==0)
gen SingleNoCollegeMom = (Female==1 & MidGenPar_Married==0 & MidGenPar_SomeCollege==0 & MidGenPar_CollegeGrad==0)
gen temp_college = (MidGenPar_SomeCollege==1 | MidGenPar_CollegeGrad==1)

sort ssuid srefmon
by ssuid srefmon: egen N_SingleMom=total(SingleMom)
by ssuid srefmon: egen N_SingleNoCollegeMom = total(SingleNoCollegeMom)
by ssuid srefmon: egen N_ParsWithCollege=total(temp_college)
by ssuid srefmon: egen N_CollegeGradPars=total(MidGenPar_CollegeGrad)
by ssuid srefmon: egen N_ParsEmployed=total(MidGenPar_Employed)
by ssuid srefmon: egen N_ParsPotentialDep=total(MidGenPar_PotentialDep)

gen AnyParsPotentialDeps=(N_ParsPotentialDep>0)
gen AnySingleMom = (N_SingleMom>0)
gen AnySingleNoCollegeMom=(N_SingleNoCollegeMom>0)
gen NoParsWithCollege = (N_ParsWithCollege==0)
gen NoCollegeGradPars = (N_CollegeGradPars==0)
gen AnyParsEmployed = (N_ParsEmployed>0)
drop temp_college

gen RefPerson=(errp==1 | errp==2)
sort ssuid srefmon
by ssuid srefmon: egen N_RefPerson=total(RefPerson)
gen MidGenHasRefPerson=(N_RefPerson>0)

merge m:1 ssuid epppnum using WellBeingVars2001_w6 
keep if _merge==3
drop _merge



gen panel=2001
save OrganizedMidGenParentData2001_w6, replace


** Extract information at household-month level about MAXIMUM amount of EITC eligibility
** for which any mid-gen parent is available.  The goal is to make this info ready 
** for merging with grandparents who live in those same households.

keep panel swave ssuid srefmon r_MaxEITC_thou r_ref_MaxEITC currentyear_r_MaxEITC_thou taxsim_eic_thou xocah AnySingleMom AnySingleNoCollegeMom NoParsWithCollege NoCollegeGradPars AnyParsEmployed MidGenHasRefPerson AnyParsPotentialDeps r_MaxFedEITC_thou ts_eic_fed
sort ssuid srefmon
by ssuid srefmon: egen r_MaxEITC_MidGenMax=max(r_MaxEITC_thou)
by ssuid srefmon: egen r_ref_MaxEITC_MidGenMax=max(r_ref_MaxEITC)
by ssuid srefmon: egen currentyear_r_MaxEITC_MidGenMax=max(currentyear_r_MaxEITC_thou)
by ssuid srefmon: egen taxsim_eic_MidGenMax=max(taxsim_eic_thou)
by ssuid srefmon: egen r_MaxFedEITC_MidGenMax=max(r_MaxFedEITC_thou)
by ssuid srefmon: egen taxsim_eic_Fed_MidGenMax=max(ts_eic_fed)

** Only keep observations of individuals whose EITC eligibility is equal to the max EITC
** eligibility of anyone in the middle generation
keep if r_MaxEITC_MidGenMax == r_MaxEITC_thou

** For later construction of a summary stats table, distinguish between cases of 1 EITC-eligible
** child and 2+ EITC-eligible children of the middle-generation parent who is eligible for the largest
** EITC amount
gen xocah_1or2plus=.
replace xocah_1or2plus = 1 if xocah==1
replace xocah_1or2plus = 2 if xocah>1 

drop xocah r_MaxEITC_thou r_ref_MaxEITC currentyear_r_MaxEITC_thou taxsim_eic_thou ts_eic_fed
duplicates drop

duplicates report ssuid srefmon

save MaxMidGenEITC2001_w6, replace
clear



** Another way to count EITC resources in the middle generation is to sum up amounts received
** by different mid-gen parents.  You don't want to double-count EITC eligibility of a husband
** and wife, but you could assume that all mothers are separate filers and sum up all EITC
** amounts of the different mid-gen mothers.  
use OrganizedMidGenParentData2001_w6

keep panel swave ssuid srefmon r_MaxEITC_thou r_ref_MaxEITC currentyear_r_MaxEITC_thou currentyear_r_ref_MaxEITC taxsim_eic_thou PotentialMidGenMom r_MaxFedEITC_thou ts_eic_fed
keep if PotentialMidGenMom==1
duplicates report ssuid srefmon
sort ssuid srefmon
by ssuid srefmon: egen Sum_MaxEITC_MidGenMoms = sum(r_MaxEITC_thou)
by ssuid srefmon: egen Sum_ref_MaxEITC_MidGenMoms = sum(r_ref_MaxEITC)
by ssuid srefmon: egen Sum_cy_MaxEITC_MidGenMoms = sum(currentyear_r_MaxEITC_thou)
by ssuid srefmon: egen Sum_cy_ref_MaxEITC_MidGenMoms = sum(currentyear_r_ref_MaxEITC)
by ssuid srefmon: egen Sum_taxsim_eic_MidGenMoms = sum(taxsim_eic_thou)
by ssuid srefmon: egen Sum_MaxFedEITC_MidGenMoms = sum(r_MaxFedEITC_thou)
by ssuid srefmon: egen Sum_taxsim_eic_Fed_MidGenMoms = sum(ts_eic_fed)

keep panel swave ssuid srefmon Sum_MaxEITC_MidGenMoms Sum_ref_MaxEITC_MidGenMoms Sum_cy_MaxEITC_MidGenMoms Sum_cy_ref_MaxEITC_MidGenMoms Sum_taxsim_eic_MidGenMoms Sum_MaxFedEITC_MidGenMoms Sum_taxsim_eic_Fed_MidGenMoms
duplicates drop
save SumMidGenEITC_2001_w6, replace
clear

**********
** Wave 9
**********

use  MidGenParentData2001_w9
** Merge in count of how many people in household identified this person as his/her mother
merge 1:1 ssuid epppnum srefmon using KidCount2001_w9
assert _merge~=1
keep if _merge==3
drop _merge


** EXPERIMENTING: Try a version where you DON'T subtract one from year
** before doing EITC calculations
gen EITC_calc_year = rhcalyr
gen xocah = DepCount
quietly do EITC_Calculator.do
drop MaxFedEITC
drop MaxStateEITC
drop StateEITC_NonRefund
rename r_MaxEITC currentyear_r_MaxEITC
rename r_MaxEITC_thou currentyear_r_MaxEITC_thou
rename r_MaxFedEITC_thou currentyear_r_MaxFedEITC_thou
rename r_MaxStateEITC_thou  currentyear_r_MaxStateEITC_thou
rename r_ref_MaxEITC  currentyear_r_ref_MaxEITC
drop real
drop EITC_calc_year xocah


gen EITC_calc_year = rhcalyr-1
gen xocah = DepCount
quietly do EITC_Calculator.do

gen YearOfTaxCalc = rhcalyr-1
gen KidsForSimulated = DepCount
replace KidsForSimulated = 3 if KidsForSimulated>3
merge m:1 YearOfTaxCalc tfipsst Married KidsForSimulated using SimulatedEITC
drop if _merge==2
drop _merge
gen taxsim_eic_thou = (ts_eic_fed + ts_eic_state)/1000


rename Married MidGenPar_Married
rename Employed MidGenPar_Employed
rename HSGrad MidGenPar_HSGrad
rename SomeCollege MidGenPar_SomeCollege
rename CollegeGrad MidGenPar_CollegeGrad
rename White MidGenPar_White
rename PotentialDep MidGenPar_PotentialDep


gen SingleMom = (Female==1 & MidGenPar_Married==0)
gen SingleNoCollegeMom = (Female==1 & MidGenPar_Married==0 & MidGenPar_SomeCollege==0 & MidGenPar_CollegeGrad==0)
gen temp_college = (MidGenPar_SomeCollege==1 | MidGenPar_CollegeGrad==1)

sort ssuid srefmon
by ssuid srefmon: egen N_SingleMom=total(SingleMom)
by ssuid srefmon: egen N_SingleNoCollegeMom = total(SingleNoCollegeMom)
by ssuid srefmon: egen N_ParsWithCollege=total(temp_college)
by ssuid srefmon: egen N_CollegeGradPars=total(MidGenPar_CollegeGrad)
by ssuid srefmon: egen N_ParsEmployed=total(MidGenPar_Employed)
by ssuid srefmon: egen N_ParsPotentialDep=total(MidGenPar_PotentialDep)

gen AnyParsPotentialDeps=(N_ParsPotentialDep>0)
gen AnySingleMom = (N_SingleMom>0)
gen AnySingleNoCollegeMom=(N_SingleNoCollegeMom>0)
gen NoParsWithCollege = (N_ParsWithCollege==0)
gen NoCollegeGradPars = (N_CollegeGradPars==0)
gen AnyParsEmployed = (N_ParsEmployed>0)
drop temp_college

gen RefPerson=(errp==1 | errp==2)
sort ssuid srefmon
by ssuid srefmon: egen N_RefPerson=total(RefPerson)
gen MidGenHasRefPerson=(N_RefPerson>0)

merge m:1 ssuid epppnum using WellBeingVars2001_w9 
keep if _merge==3
drop _merge

gen panel=2001
save OrganizedMidGenParentData2001_w9, replace


** Extract information at household-month level about MAXIMUM amount of EITC eligibility
** for which any mid-gen parent is available.  The goal is to make this info ready 
** for merging with grandparents who live in those same households.

keep panel swave ssuid srefmon r_MaxEITC_thou r_ref_MaxEITC currentyear_r_MaxEITC_thou taxsim_eic_thou xocah AnySingleMom AnySingleNoCollegeMom NoParsWithCollege NoCollegeGradPars AnyParsEmployed MidGenHasRefPerson AnyParsPotentialDeps r_MaxFedEITC_thou ts_eic_fed
sort ssuid srefmon
by ssuid srefmon: egen r_MaxEITC_MidGenMax=max(r_MaxEITC_thou)
by ssuid srefmon: egen r_ref_MaxEITC_MidGenMax=max(r_ref_MaxEITC)
by ssuid srefmon: egen currentyear_r_MaxEITC_MidGenMax=max(currentyear_r_MaxEITC_thou)
by ssuid srefmon: egen taxsim_eic_MidGenMax=max(taxsim_eic_thou)
by ssuid srefmon: egen r_MaxFedEITC_MidGenMax=max(r_MaxFedEITC_thou)
by ssuid srefmon: egen taxsim_eic_Fed_MidGenMax=max(ts_eic_fed)

** Only keep observations of individuals whose EITC eligibility is equal to the max EITC
** eligibility of anyone in the middle generation
keep if r_MaxEITC_MidGenMax == r_MaxEITC_thou

** For later construction of a summary stats table, distinguish between cases of 1 EITC-eligible
** child and 2+ EITC-eligible children of the middle-generation parent who is eligible for the largest
** EITC amount
gen xocah_1or2plus=.
replace xocah_1or2plus = 1 if xocah==1
replace xocah_1or2plus = 2 if xocah>1 

drop xocah r_MaxEITC_thou r_ref_MaxEITC currentyear_r_MaxEITC_thou taxsim_eic_thou ts_eic_fed
duplicates drop

duplicates report ssuid srefmon

save MaxMidGenEITC2001_w9, replace
clear



** Another way to count EITC resources in the middle generation is to sum up amounts received
** by different mid-gen parents.  You don't want to double-count EITC eligibility of a husband
** and wife, but you could assume that all mothers are separate filers and sum up all EITC
** amounts of the different mid-gen mothers.  
use OrganizedMidGenParentData2001_w9

keep panel swave ssuid srefmon r_MaxEITC_thou r_ref_MaxEITC currentyear_r_MaxEITC_thou currentyear_r_ref_MaxEITC taxsim_eic_thou PotentialMidGenMom r_MaxFedEITC_thou ts_eic_fed
keep if PotentialMidGenMom==1
duplicates report ssuid srefmon
sort ssuid srefmon
by ssuid srefmon: egen Sum_MaxEITC_MidGenMoms = sum(r_MaxEITC_thou)
by ssuid srefmon: egen Sum_ref_MaxEITC_MidGenMoms = sum(r_ref_MaxEITC)
by ssuid srefmon: egen Sum_cy_MaxEITC_MidGenMoms = sum(currentyear_r_MaxEITC_thou)
by ssuid srefmon: egen Sum_cy_ref_MaxEITC_MidGenMoms = sum(currentyear_r_ref_MaxEITC)
by ssuid srefmon: egen Sum_taxsim_eic_MidGenMoms = sum(taxsim_eic_thou)
by ssuid srefmon: egen Sum_MaxFedEITC_MidGenMoms = sum(r_MaxFedEITC_thou)
by ssuid srefmon: egen Sum_taxsim_eic_Fed_MidGenMoms = sum(ts_eic_fed)

keep panel swave ssuid srefmon Sum_MaxEITC_MidGenMoms Sum_ref_MaxEITC_MidGenMoms Sum_cy_MaxEITC_MidGenMoms Sum_cy_ref_MaxEITC_MidGenMoms Sum_taxsim_eic_MidGenMoms Sum_MaxFedEITC_MidGenMoms Sum_taxsim_eic_Fed_MidGenMoms
duplicates drop
save SumMidGenEITC_2001_w9, replace
clear

*******************
** 2004
*******************

**********
** Wave 3
**********

use  MidGenParentData2004_w3
** Merge in count of how many people in household identified this person as his/her mother
merge 1:1 ssuid epppnum srefmon using KidCount2004_w3
assert _merge~=1
keep if _merge==3
drop _merge


** EXPERIMENTING: Try a version where you DON'T subtract one from year
** before doing EITC calculations
gen EITC_calc_year = rhcalyr
gen xocah = DepCount
quietly do EITC_Calculator.do
drop MaxFedEITC
drop MaxStateEITC
drop StateEITC_NonRefund
rename r_MaxEITC currentyear_r_MaxEITC
rename r_MaxEITC_thou currentyear_r_MaxEITC_thou
rename r_MaxFedEITC_thou currentyear_r_MaxFedEITC_thou
rename r_MaxStateEITC_thou  currentyear_r_MaxStateEITC_thou
rename r_ref_MaxEITC  currentyear_r_ref_MaxEITC
drop real
drop EITC_calc_year xocah

gen EITC_calc_year = rhcalyr-1
gen xocah = DepCount
quietly do EITC_Calculator.do

gen YearOfTaxCalc = rhcalyr-1
gen KidsForSimulated = DepCount
replace KidsForSimulated = 3 if KidsForSimulated>3
merge m:1 YearOfTaxCalc tfipsst Married KidsForSimulated using SimulatedEITC
drop if _merge==2
drop _merge
gen taxsim_eic_thou = (ts_eic_fed + ts_eic_state)/1000


rename Married MidGenPar_Married
rename Employed MidGenPar_Employed
rename HSGrad MidGenPar_HSGrad
rename SomeCollege MidGenPar_SomeCollege
rename CollegeGrad MidGenPar_CollegeGrad
rename White MidGenPar_White
rename PotentialDep MidGenPar_PotentialDep


gen SingleMom = (Female==1 & MidGenPar_Married==0)
gen SingleNoCollegeMom = (Female==1 & MidGenPar_Married==0 & MidGenPar_SomeCollege==0 & MidGenPar_CollegeGrad==0)
gen temp_college = (MidGenPar_SomeCollege==1 | MidGenPar_CollegeGrad==1)


sort ssuid srefmon
by ssuid srefmon: egen N_SingleMom=total(SingleMom)
by ssuid srefmon: egen N_SingleNoCollegeMom = total(SingleNoCollegeMom)
by ssuid srefmon: egen N_ParsWithCollege=total(temp_college)
by ssuid srefmon: egen N_CollegeGradPars=total(MidGenPar_CollegeGrad)
by ssuid srefmon: egen N_ParsEmployed=total(MidGenPar_Employed)
by ssuid srefmon: egen N_ParsPotentialDep=total(MidGenPar_PotentialDep)

gen AnyParsPotentialDeps=(N_ParsPotentialDep>0)
gen AnySingleMom = (N_SingleMom>0)
gen AnySingleNoCollegeMom=(N_SingleNoCollegeMom>0)
gen NoParsWithCollege = (N_ParsWithCollege==0)
gen NoCollegeGradPars = (N_CollegeGradPars==0)
gen AnyParsEmployed = (N_ParsEmployed>0)
drop temp_college

gen RefPerson=(errp==1 | errp==2)
sort ssuid srefmon
by ssuid srefmon: egen N_RefPerson=total(RefPerson)
gen MidGenHasRefPerson=(N_RefPerson>0)

merge m:1 ssuid epppnum using WellBeingVars2004_w3 
keep if _merge==3
drop _merge


*******************************************************************************
** Merge in data on receipt of childcare from a grandparent 

merge m:1 ssuid epppnum using ChildCareVars_2004_w4
drop if _merge==2
drop _merge 

sort ssuid srefmon
by ssuid srefmon: egen N_GetCCFromGP=total(GetCCFromGP)
gen AnyParsGetCCFromGP = (N_GetCCFromGP>0)


gen panel=2004
save OrganizedMidGenParentData2004_w3, replace


** Extract information at household-month level about MAXIMUM amount of EITC eligibility
** for which any mid-gen parent is available.  The goal is to make this info ready 
** for merging with grandparents who live in those same households.

keep panel swave ssuid srefmon r_MaxEITC_thou r_ref_MaxEITC currentyear_r_MaxEITC_thou taxsim_eic_thou xocah AnySingleMom AnySingleNoCollegeMom NoParsWithCollege NoCollegeGradPars AnyParsEmployed MidGenHasRefPerson AnyParsPotentialDeps r_MaxFedEITC_thou ts_eic_fed AnyParsGetCCFromGP
sort ssuid srefmon
by ssuid srefmon: egen r_MaxEITC_MidGenMax=max(r_MaxEITC_thou)
by ssuid srefmon: egen r_ref_MaxEITC_MidGenMax=max(r_ref_MaxEITC)
by ssuid srefmon: egen currentyear_r_MaxEITC_MidGenMax=max(currentyear_r_MaxEITC_thou)
by ssuid srefmon: egen taxsim_eic_MidGenMax=max(taxsim_eic_thou)
by ssuid srefmon: egen r_MaxFedEITC_MidGenMax=max(r_MaxFedEITC_thou)
by ssuid srefmon: egen taxsim_eic_Fed_MidGenMax=max(ts_eic_fed)

** Only keep observations of individuals whose EITC eligibility is equal to the max EITC
** eligibility of anyone in the middle generation
keep if r_MaxEITC_MidGenMax == r_MaxEITC_thou

** For later construction of a summary stats table, distinguish between cases of 1 EITC-eligible
** child and 2+ EITC-eligible children of the middle-generation parent who is eligible for the largest
** EITC amount
gen xocah_1or2plus=.
replace xocah_1or2plus = 1 if xocah==1
replace xocah_1or2plus = 2 if xocah>1 

drop xocah r_MaxEITC_thou r_ref_MaxEITC currentyear_r_MaxEITC_thou taxsim_eic_thou ts_eic_fed
duplicates drop

duplicates report ssuid srefmon

save MaxMidGenEITC2004_w3, replace
clear


** Another way to count EITC resources in the middle generation is to sum up amounts received
** by different mid-gen parents.  You don't want to double-count EITC eligibility of a husband
** and wife, but you could assume that all mothers are separate filers and sum up all EITC
** amounts of the different mid-gen mothers.  
use OrganizedMidGenParentData2004_w3

keep panel swave ssuid srefmon r_MaxEITC_thou r_ref_MaxEITC currentyear_r_MaxEITC_thou currentyear_r_ref_MaxEITC taxsim_eic_thou PotentialMidGenMom r_MaxFedEITC_thou ts_eic_fed
keep if PotentialMidGenMom==1
duplicates report ssuid srefmon
sort ssuid srefmon
by ssuid srefmon: egen Sum_MaxEITC_MidGenMoms = sum(r_MaxEITC_thou)
by ssuid srefmon: egen Sum_ref_MaxEITC_MidGenMoms = sum(r_ref_MaxEITC)
by ssuid srefmon: egen Sum_cy_MaxEITC_MidGenMoms = sum(currentyear_r_MaxEITC_thou)
by ssuid srefmon: egen Sum_cy_ref_MaxEITC_MidGenMoms = sum(currentyear_r_ref_MaxEITC)
by ssuid srefmon: egen Sum_taxsim_eic_MidGenMoms = sum(taxsim_eic_thou)
by ssuid srefmon: egen Sum_MaxFedEITC_MidGenMoms = sum(r_MaxFedEITC_thou)
by ssuid srefmon: egen Sum_taxsim_eic_Fed_MidGenMoms = sum(ts_eic_fed)

keep panel swave ssuid srefmon Sum_MaxEITC_MidGenMoms Sum_ref_MaxEITC_MidGenMoms Sum_cy_MaxEITC_MidGenMoms Sum_cy_ref_MaxEITC_MidGenMoms Sum_taxsim_eic_MidGenMoms Sum_MaxFedEITC_MidGenMoms Sum_taxsim_eic_Fed_MidGenMoms
duplicates drop
save SumMidGenEITC_2004_w3, replace
clear

********** 
** Wave 6
**********

use  MidGenParentData2004_w6
** Merge in count of how many people in household identified this person as his/her mother
merge 1:1 ssuid epppnum srefmon using KidCount2004_w6
assert _merge~=1
keep if _merge==3
drop _merge


** EXPERIMENTING: Try a version where you DON'T subtract one from year
** before doing EITC calculations
gen EITC_calc_year = rhcalyr
gen xocah = DepCount
quietly do EITC_Calculator.do
drop MaxFedEITC
drop MaxStateEITC
drop StateEITC_NonRefund
rename r_MaxEITC currentyear_r_MaxEITC
rename r_MaxEITC_thou currentyear_r_MaxEITC_thou
rename r_MaxFedEITC_thou currentyear_r_MaxFedEITC_thou
rename r_MaxStateEITC_thou  currentyear_r_MaxStateEITC_thou
rename r_ref_MaxEITC  currentyear_r_ref_MaxEITC
drop real
drop EITC_calc_year xocah


gen EITC_calc_year = rhcalyr-1
gen xocah = DepCount
quietly do EITC_Calculator.do

gen YearOfTaxCalc = rhcalyr-1
gen KidsForSimulated = DepCount
replace KidsForSimulated = 3 if KidsForSimulated>3
merge m:1 YearOfTaxCalc tfipsst Married KidsForSimulated using SimulatedEITC
drop if _merge==2
drop _merge
gen taxsim_eic_thou = (ts_eic_fed + ts_eic_state)/1000


rename Married MidGenPar_Married
rename Employed MidGenPar_Employed
rename HSGrad MidGenPar_HSGrad
rename SomeCollege MidGenPar_SomeCollege
rename CollegeGrad MidGenPar_CollegeGrad
rename White MidGenPar_White
rename PotentialDep MidGenPar_PotentialDep


gen SingleMom = (Female==1 & MidGenPar_Married==0)
gen SingleNoCollegeMom = (Female==1 & MidGenPar_Married==0 & MidGenPar_SomeCollege==0 & MidGenPar_CollegeGrad==0)
gen temp_college = (MidGenPar_SomeCollege==1 | MidGenPar_CollegeGrad==1)


sort ssuid srefmon
by ssuid srefmon: egen N_SingleMom=total(SingleMom)
by ssuid srefmon: egen N_SingleNoCollegeMom = total(SingleNoCollegeMom)
by ssuid srefmon: egen N_ParsWithCollege=total(temp_college)
by ssuid srefmon: egen N_CollegeGradPars=total(MidGenPar_CollegeGrad)
by ssuid srefmon: egen N_ParsEmployed=total(MidGenPar_Employed)
by ssuid srefmon: egen N_ParsPotentialDep=total(MidGenPar_PotentialDep)

gen AnyParsPotentialDeps=(N_ParsPotentialDep>0)
gen AnySingleMom = (N_SingleMom>0)
gen AnySingleNoCollegeMom=(N_SingleNoCollegeMom>0)
gen NoParsWithCollege = (N_ParsWithCollege==0)
gen NoCollegeGradPars = (N_CollegeGradPars==0)
gen AnyParsEmployed = (N_ParsEmployed>0)
drop temp_college

gen RefPerson=(errp==1 | errp==2)
sort ssuid srefmon
by ssuid srefmon: egen N_RefPerson=total(RefPerson)
gen MidGenHasRefPerson=(N_RefPerson>0)

merge m:1 ssuid epppnum using WellBeingVars2004_w6 
keep if _merge==3
drop _merge



gen panel=2004
save OrganizedMidGenParentData2004_w6, replace


** Extract information at household-month level about MAXIMUM amount of EITC eligibility
** for which any mid-gen parent is available.  The goal is to make this info ready 
** for merging with grandparents who live in those same households.

keep panel swave ssuid srefmon r_MaxEITC_thou r_ref_MaxEITC currentyear_r_MaxEITC_thou taxsim_eic_thou xocah AnySingleMom AnySingleNoCollegeMom NoParsWithCollege NoCollegeGradPars AnyParsEmployed MidGenHasRefPerson AnyParsPotentialDeps r_MaxFedEITC_thou ts_eic_fed
sort ssuid srefmon
by ssuid srefmon: egen r_MaxEITC_MidGenMax=max(r_MaxEITC_thou)
by ssuid srefmon: egen r_ref_MaxEITC_MidGenMax=max(r_ref_MaxEITC)
by ssuid srefmon: egen currentyear_r_MaxEITC_MidGenMax=max(currentyear_r_MaxEITC_thou)
by ssuid srefmon: egen taxsim_eic_MidGenMax=max(taxsim_eic_thou)
by ssuid srefmon: egen r_MaxFedEITC_MidGenMax=max(r_MaxFedEITC_thou)
by ssuid srefmon: egen taxsim_eic_Fed_MidGenMax=max(ts_eic_fed)

** Only keep observations of individuals whose EITC eligibility is equal to the max EITC
** eligibility of anyone in the middle generation
keep if r_MaxEITC_MidGenMax == r_MaxEITC_thou

** For later construction of a summary stats table, distinguish between cases of 1 EITC-eligible
** child and 2+ EITC-eligible children of the middle-generation parent who is eligible for the largest
** EITC amount
gen xocah_1or2plus=.
replace xocah_1or2plus = 1 if xocah==1
replace xocah_1or2plus = 2 if xocah>1 

drop xocah r_MaxEITC_thou r_ref_MaxEITC currentyear_r_MaxEITC_thou taxsim_eic_thou ts_eic_fed
duplicates drop

duplicates report ssuid srefmon

save MaxMidGenEITC2004_w6, replace
clear



** Another way to count EITC resources in the middle generation is to sum up amounts received
** by different mid-gen parents.  You don't want to double-count EITC eligibility of a husband
** and wife, but you could assume that all mothers are separate filers and sum up all EITC
** amounts of the different mid-gen mothers.  
use OrganizedMidGenParentData2004_w6

keep panel swave ssuid srefmon r_MaxEITC_thou r_ref_MaxEITC currentyear_r_MaxEITC_thou currentyear_r_ref_MaxEITC taxsim_eic_thou PotentialMidGenMom r_MaxFedEITC_thou ts_eic_fed
keep if PotentialMidGenMom==1
duplicates report ssuid srefmon
sort ssuid srefmon
by ssuid srefmon: egen Sum_MaxEITC_MidGenMoms = sum(r_MaxEITC_thou)
by ssuid srefmon: egen Sum_ref_MaxEITC_MidGenMoms = sum(r_ref_MaxEITC)
by ssuid srefmon: egen Sum_cy_MaxEITC_MidGenMoms = sum(currentyear_r_MaxEITC_thou)
by ssuid srefmon: egen Sum_cy_ref_MaxEITC_MidGenMoms = sum(currentyear_r_ref_MaxEITC)
by ssuid srefmon: egen Sum_taxsim_eic_MidGenMoms = sum(taxsim_eic_thou)
by ssuid srefmon: egen Sum_MaxFedEITC_MidGenMoms = sum(r_MaxFedEITC_thou)
by ssuid srefmon: egen Sum_taxsim_eic_Fed_MidGenMoms = sum(ts_eic_fed)

keep panel swave ssuid srefmon Sum_MaxEITC_MidGenMoms Sum_ref_MaxEITC_MidGenMoms Sum_cy_MaxEITC_MidGenMoms Sum_cy_ref_MaxEITC_MidGenMoms Sum_taxsim_eic_MidGenMoms Sum_MaxFedEITC_MidGenMoms Sum_taxsim_eic_Fed_MidGenMoms
duplicates drop
save SumMidGenEITC_2004_w6, replace
clear


*********************
** 2008
*********************

***********
** Wave 4
***********

use  MidGenParentData2008_w4
** Merge in count of how many people in household identified this person as his/her mother
merge 1:1 ssuid epppnum srefmon using KidCount2008_w4
assert _merge~=1
keep if _merge==3
drop _merge



** EXPERIMENTING: Try a version where you DON'T subtract one from year
** before doing EITC calculations
gen EITC_calc_year = rhcalyr
gen xocah = DepCount
quietly do EITC_Calculator.do
drop MaxFedEITC
drop MaxStateEITC
drop StateEITC_NonRefund
rename r_MaxEITC currentyear_r_MaxEITC
rename r_MaxEITC_thou currentyear_r_MaxEITC_thou
rename r_MaxFedEITC_thou currentyear_r_MaxFedEITC_thou
rename r_MaxStateEITC_thou  currentyear_r_MaxStateEITC_thou
rename r_ref_MaxEITC  currentyear_r_ref_MaxEITC
drop real
drop EITC_calc_year xocah

gen EITC_calc_year = rhcalyr-1
gen xocah = DepCount
quietly do EITC_Calculator.do

gen YearOfTaxCalc = rhcalyr-1
gen KidsForSimulated = DepCount
replace KidsForSimulated = 3 if KidsForSimulated>3
merge m:1 YearOfTaxCalc tfipsst Married KidsForSimulated using SimulatedEITC
drop if _merge==2
drop _merge
gen taxsim_eic_thou = (ts_eic_fed + ts_eic_state)/1000


rename Married MidGenPar_Married
rename Employed MidGenPar_Employed
rename HSGrad MidGenPar_HSGrad
rename SomeCollege MidGenPar_SomeCollege
rename CollegeGrad MidGenPar_CollegeGrad
rename White MidGenPar_White
rename PotentialDep MidGenPar_PotentialDep


gen SingleMom = (Female==1 & MidGenPar_Married==0)
gen SingleNoCollegeMom = (Female==1 & MidGenPar_Married==0 & MidGenPar_SomeCollege==0 & MidGenPar_CollegeGrad==0)
gen temp_college = (MidGenPar_SomeCollege==1 | MidGenPar_CollegeGrad==1)


sort ssuid srefmon
by ssuid srefmon: egen N_SingleMom=total(SingleMom)
by ssuid srefmon: egen N_SingleNoCollegeMom = total(SingleNoCollegeMom)
by ssuid srefmon: egen N_ParsWithCollege=total(temp_college)
by ssuid srefmon: egen N_CollegeGradPars=total(MidGenPar_CollegeGrad)
by ssuid srefmon: egen N_ParsEmployed=total(MidGenPar_Employed)
by ssuid srefmon: egen N_ParsPotentialDep=total(MidGenPar_PotentialDep)

gen AnyParsPotentialDeps=(N_ParsPotentialDep>0)
gen AnySingleMom = (N_SingleMom>0)
gen AnySingleNoCollegeMom=(N_SingleNoCollegeMom>0)
gen NoParsWithCollege = (N_ParsWithCollege==0)
gen NoCollegeGradPars = (N_CollegeGradPars==0)
gen AnyParsEmployed = (N_ParsEmployed>0)
drop temp_college

gen RefPerson=(errp==1 | errp==2)
sort ssuid srefmon
by ssuid srefmon: egen N_RefPerson=total(RefPerson)
gen MidGenHasRefPerson=(N_RefPerson>0)

merge m:1 ssuid epppnum using WellBeingVars2008_w4 
keep if _merge==3
drop _merge

*



*******************************************************************************
** Merge in data on receipt of childcare from a grandparent 

merge m:1 ssuid epppnum using ChildCareVars_2008_w5
drop if _merge==2
drop _merge 

sort ssuid srefmon
by ssuid srefmon: egen N_GetCCFromGP=total(GetCCFromGP)
gen AnyParsGetCCFromGP = (N_GetCCFromGP>0)


gen panel=2008
save OrganizedMidGenParentData2008_w4, replace


** Extract information at household-month level about MAXIMUM amount of EITC eligibility
** for which any mid-gen parent is available.  The goal is to make this info ready 
** for merging with grandparents who live in those same households.

keep panel swave ssuid srefmon r_MaxEITC_thou r_ref_MaxEITC currentyear_r_MaxEITC_thou taxsim_eic_thou xocah AnySingleMom AnySingleNoCollegeMom NoParsWithCollege NoCollegeGradPars AnyParsEmployed MidGenHasRefPerson AnyParsPotentialDeps r_MaxFedEITC_thou ts_eic_fed AnyParsGetCCFromGP
sort ssuid srefmon
by ssuid srefmon: egen r_MaxEITC_MidGenMax=max(r_MaxEITC_thou)
by ssuid srefmon: egen r_ref_MaxEITC_MidGenMax=max(r_ref_MaxEITC)
by ssuid srefmon: egen currentyear_r_MaxEITC_MidGenMax=max(currentyear_r_MaxEITC_thou)
by ssuid srefmon: egen taxsim_eic_MidGenMax=max(taxsim_eic_thou)
by ssuid srefmon: egen r_MaxFedEITC_MidGenMax=max(r_MaxFedEITC_thou)
by ssuid srefmon: egen taxsim_eic_Fed_MidGenMax=max(ts_eic_fed)

** Only keep observations of individuals whose EITC eligibility is equal to the max EITC
** eligibility of anyone in the middle generation
keep if r_MaxEITC_MidGenMax == r_MaxEITC_thou

** For later construction of a summary stats table, distinguish between cases of 1 EITC-eligible
** child and 2+ EITC-eligible children of the middle-generation parent who is eligible for the largest
** EITC amount
gen xocah_1or2plus=.
replace xocah_1or2plus = 1 if xocah==1
replace xocah_1or2plus = 2 if xocah>1 

drop xocah r_MaxEITC_thou r_ref_MaxEITC currentyear_r_MaxEITC_thou taxsim_eic_thou ts_eic_fed
duplicates drop

duplicates report ssuid srefmon

save MaxMidGenEITC2008_w4, replace
clear




** Another way to count EITC resources in the middle generation is to sum up amounts received
** by different mid-gen parents.  You don't want to double-count EITC eligibility of a husband
** and wife, but you could assume that all mothers are separate filers and sum up all EITC
** amounts of the different mid-gen mothers.  
use OrganizedMidGenParentData2008_w4

keep panel swave ssuid srefmon r_MaxEITC_thou r_ref_MaxEITC currentyear_r_MaxEITC_thou currentyear_r_ref_MaxEITC taxsim_eic_thou PotentialMidGenMom r_MaxFedEITC_thou ts_eic_fed
keep if PotentialMidGenMom==1
duplicates report ssuid srefmon
sort ssuid srefmon
by ssuid srefmon: egen Sum_MaxEITC_MidGenMoms = sum(r_MaxEITC_thou)
by ssuid srefmon: egen Sum_ref_MaxEITC_MidGenMoms = sum(r_ref_MaxEITC)
by ssuid srefmon: egen Sum_cy_MaxEITC_MidGenMoms = sum(currentyear_r_MaxEITC_thou)
by ssuid srefmon: egen Sum_cy_ref_MaxEITC_MidGenMoms = sum(currentyear_r_ref_MaxEITC)
by ssuid srefmon: egen Sum_taxsim_eic_MidGenMoms = sum(taxsim_eic_thou)
by ssuid srefmon: egen Sum_MaxFedEITC_MidGenMoms = sum(r_MaxFedEITC_thou)
by ssuid srefmon: egen Sum_taxsim_eic_Fed_MidGenMoms = sum(ts_eic_fed)

keep panel swave ssuid srefmon Sum_MaxEITC_MidGenMoms Sum_ref_MaxEITC_MidGenMoms Sum_cy_MaxEITC_MidGenMoms Sum_cy_ref_MaxEITC_MidGenMoms Sum_taxsim_eic_MidGenMoms Sum_MaxFedEITC_MidGenMoms Sum_taxsim_eic_Fed_MidGenMoms
duplicates drop
save SumMidGenEITC_2008_w4, replace
clear


***********
** Wave 7
***********

use  MidGenParentData2008_w7
** Merge in count of how many people in household identified this person as his/her mother
merge 1:1 ssuid epppnum srefmon using KidCount2008_w7
assert _merge~=1
keep if _merge==3
drop _merge


** EXPERIMENTING: Try a version where you DON'T subtract one from year
** before doing EITC calculations
gen EITC_calc_year = rhcalyr
gen xocah = DepCount
quietly do EITC_Calculator.do
drop MaxFedEITC
drop MaxStateEITC
drop StateEITC_NonRefund
rename r_MaxEITC currentyear_r_MaxEITC
rename r_MaxEITC_thou currentyear_r_MaxEITC_thou
rename r_MaxFedEITC_thou currentyear_r_MaxFedEITC_thou
rename r_MaxStateEITC_thou  currentyear_r_MaxStateEITC_thou
rename r_ref_MaxEITC  currentyear_r_ref_MaxEITC
drop real
drop EITC_calc_year xocah

gen EITC_calc_year = rhcalyr-1
gen xocah = DepCount
quietly do EITC_Calculator.do

gen YearOfTaxCalc = rhcalyr-1
gen KidsForSimulated = DepCount
replace KidsForSimulated = 3 if KidsForSimulated>3
merge m:1 YearOfTaxCalc tfipsst Married KidsForSimulated using SimulatedEITC
drop if _merge==2
drop _merge
gen taxsim_eic_thou = (ts_eic_fed + ts_eic_state)/1000


rename Married MidGenPar_Married
rename Employed MidGenPar_Employed
rename HSGrad MidGenPar_HSGrad
rename SomeCollege MidGenPar_SomeCollege
rename CollegeGrad MidGenPar_CollegeGrad
rename White MidGenPar_White
rename PotentialDep MidGenPar_PotentialDep


gen SingleMom = (Female==1 & MidGenPar_Married==0)
gen SingleNoCollegeMom = (Female==1 & MidGenPar_Married==0 & MidGenPar_SomeCollege==0 & MidGenPar_CollegeGrad==0)
gen temp_college = (MidGenPar_SomeCollege==1 | MidGenPar_CollegeGrad==1)


sort ssuid srefmon
by ssuid srefmon: egen N_SingleMom=total(SingleMom)
by ssuid srefmon: egen N_SingleNoCollegeMom = total(SingleNoCollegeMom)
by ssuid srefmon: egen N_ParsWithCollege=total(temp_college)
by ssuid srefmon: egen N_CollegeGradPars=total(MidGenPar_CollegeGrad)
by ssuid srefmon: egen N_ParsEmployed=total(MidGenPar_Employed)
by ssuid srefmon: egen N_ParsPotentialDep=total(MidGenPar_PotentialDep)

gen AnyParsPotentialDeps=(N_ParsPotentialDep>0)
gen AnySingleMom = (N_SingleMom>0)
gen AnySingleNoCollegeMom=(N_SingleNoCollegeMom>0)
gen NoParsWithCollege = (N_ParsWithCollege==0)
gen NoCollegeGradPars = (N_CollegeGradPars==0)
gen AnyParsEmployed = (N_ParsEmployed>0)
drop temp_college

gen RefPerson=(errp==1 | errp==2)
sort ssuid srefmon
by ssuid srefmon: egen N_RefPerson=total(RefPerson)
gen MidGenHasRefPerson=(N_RefPerson>0)

merge m:1 ssuid epppnum using WellBeingVars2008_w7 
keep if _merge==3
drop _merge

*

*******************************************************************************
** Merge in data on tax filing and EITC claiming.  
** NOTE: The tax questions and the other questions of interest aren't asked
** in the same wave. Merge well-being, family structure, etc. data from a given 
** wave with tax data at the closest subsequent wave.

 merge m:1 ssuid epppnum using TaxVars2008_w8
 

 
 ** Does anyone in the household say they filed a tax return?
 ** NOTE: Will probably need different treatment of missing data, rather
 ** than just assuming anyone with missing data did not file.
sort ssuid
by ssuid: egen N_Filed = total(Filed)
gen AnyoneInHHFiled = (N_Filed>0 & N_Filed~=.) 
drop N_Filed
 
 ** Does anyone in the household say they claimed the EITC?
 sort ssuid
by ssuid: egen N_ClaimedEITC = total(ClaimedEITC)
gen AnyoneInHHClaimed = (N_ClaimedEITC>0 & N_ClaimedEITC~=.)
drop N_ClaimedEITC

** Probably don't want to keep anyone who only appeared in the using data. 
** You need health and debt variables as outcomes of interest, and anyone
** who is only in the using data won't have these variables
drop if _merge==2
drop _merge


*******************************************************************************
** Merge in data on receipt of childcare from a grandparent 

merge m:1 ssuid epppnum using ChildCareVars_2008_w8
drop if _merge==2
drop _merge 

sort ssuid srefmon
by ssuid srefmon: egen N_GetCCFromGP=total(GetCCFromGP)
gen AnyParsGetCCFromGP = (N_GetCCFromGP>0)

gen panel=2008
save OrganizedMidGenParentData2008_w7, replace


** Extract information at household-month level about MAXIMUM amount of EITC eligibility
** for which any mid-gen parent is available.  The goal is to make this info ready 
** for merging with grandparents who live in those same households.

keep panel swave ssuid srefmon r_MaxEITC_thou r_ref_MaxEITC currentyear_r_MaxEITC_thou taxsim_eic_thou xocah AnySingleMom AnySingleNoCollegeMom NoParsWithCollege NoCollegeGradPars AnyParsEmployed MidGenHasRefPerson AnyParsPotentialDeps r_MaxFedEITC_thou ts_eic_fed AnyParsGetCCFromGP
sort ssuid srefmon
by ssuid srefmon: egen r_MaxEITC_MidGenMax=max(r_MaxEITC_thou)
by ssuid srefmon: egen r_ref_MaxEITC_MidGenMax=max(r_ref_MaxEITC)
by ssuid srefmon: egen currentyear_r_MaxEITC_MidGenMax=max(currentyear_r_MaxEITC_thou)
by ssuid srefmon: egen taxsim_eic_MidGenMax=max(taxsim_eic_thou)
by ssuid srefmon: egen r_MaxFedEITC_MidGenMax=max(r_MaxFedEITC_thou)
by ssuid srefmon: egen taxsim_eic_Fed_MidGenMax=max(ts_eic_fed)

** Only keep observations of individuals whose EITC eligibility is equal to the max EITC
** eligibility of anyone in the middle generation
keep if r_MaxEITC_MidGenMax == r_MaxEITC_thou

** For later construction of a summary stats table, distinguish between cases of 1 EITC-eligible
** child and 2+ EITC-eligible children of the middle-generation parent who is eligible for the largest
** EITC amount
gen xocah_1or2plus=.
replace xocah_1or2plus = 1 if xocah==1
replace xocah_1or2plus = 2 if xocah>1 

drop xocah r_MaxEITC_thou r_ref_MaxEITC currentyear_r_MaxEITC_thou taxsim_eic_thou ts_eic_fed
duplicates drop

duplicates report ssuid srefmon

save MaxMidGenEITC2008_w7, replace
clear


** Another way to count EITC resources in the middle generation is to sum up amounts received
** by different mid-gen parents.  You don't want to double-count EITC eligibility of a husband
** and wife, but you could assume that all mothers are separate filers and sum up all EITC
** amounts of the different mid-gen mothers.  
use OrganizedMidGenParentData2008_w7

keep panel swave ssuid srefmon r_MaxEITC_thou r_ref_MaxEITC currentyear_r_MaxEITC_thou currentyear_r_ref_MaxEITC taxsim_eic_thou PotentialMidGenMom r_MaxFedEITC_thou ts_eic_fed
keep if PotentialMidGenMom==1
duplicates report ssuid srefmon
sort ssuid srefmon
by ssuid srefmon: egen Sum_MaxEITC_MidGenMoms = sum(r_MaxEITC_thou)
by ssuid srefmon: egen Sum_ref_MaxEITC_MidGenMoms = sum(r_ref_MaxEITC)
by ssuid srefmon: egen Sum_cy_MaxEITC_MidGenMoms = sum(currentyear_r_MaxEITC_thou)
by ssuid srefmon: egen Sum_cy_ref_MaxEITC_MidGenMoms = sum(currentyear_r_ref_MaxEITC)
by ssuid srefmon: egen Sum_taxsim_eic_MidGenMoms = sum(taxsim_eic_thou)
by ssuid srefmon: egen Sum_MaxFedEITC_MidGenMoms = sum(r_MaxFedEITC_thou)
by ssuid srefmon: egen Sum_taxsim_eic_Fed_MidGenMoms = sum(ts_eic_fed)

keep panel swave ssuid srefmon Sum_MaxEITC_MidGenMoms Sum_ref_MaxEITC_MidGenMoms Sum_cy_MaxEITC_MidGenMoms Sum_cy_ref_MaxEITC_MidGenMoms Sum_taxsim_eic_MidGenMoms Sum_MaxFedEITC_MidGenMoms Sum_taxsim_eic_Fed_MidGenMoms
duplicates drop
save SumMidGenEITC_2008_w7, replace
clear



***********
** Wave 10
***********

use  MidGenParentData2008_w10
** Merge in count of how many people in household identified this person as his/her mother
merge 1:1 ssuid epppnum srefmon using KidCount2008_w10
assert _merge~=1
keep if _merge==3
drop _merge


** EXPERIMENTING: Try a version where you DON'T subtract one from year
** before doing EITC calculations
gen EITC_calc_year = rhcalyr
gen xocah = DepCount
quietly do EITC_Calculator.do
drop MaxFedEITC
drop MaxStateEITC
drop StateEITC_NonRefund
rename r_MaxEITC currentyear_r_MaxEITC
rename r_MaxEITC_thou currentyear_r_MaxEITC_thou
rename r_MaxFedEITC_thou currentyear_r_MaxFedEITC_thou
rename r_MaxStateEITC_thou  currentyear_r_MaxStateEITC_thou
rename r_ref_MaxEITC  currentyear_r_ref_MaxEITC
drop real
drop EITC_calc_year xocah


gen EITC_calc_year = rhcalyr-1
gen xocah = DepCount
quietly do EITC_Calculator.do

gen YearOfTaxCalc = rhcalyr-1
gen KidsForSimulated = DepCount
replace KidsForSimulated = 3 if KidsForSimulated>3
merge m:1 YearOfTaxCalc tfipsst Married KidsForSimulated using SimulatedEITC
drop if _merge==2
drop _merge
gen taxsim_eic_thou = (ts_eic_fed + ts_eic_state)/1000


rename Married MidGenPar_Married
rename Employed MidGenPar_Employed
rename HSGrad MidGenPar_HSGrad
rename SomeCollege MidGenPar_SomeCollege
rename CollegeGrad MidGenPar_CollegeGrad
rename White MidGenPar_White
rename PotentialDep MidGenPar_PotentialDep


gen SingleMom = (Female==1 & MidGenPar_Married==0)
gen SingleNoCollegeMom = (Female==1 & MidGenPar_Married==0 & MidGenPar_SomeCollege==0 & MidGenPar_CollegeGrad==0)
gen temp_college = (MidGenPar_SomeCollege==1 | MidGenPar_CollegeGrad==1)


sort ssuid srefmon
by ssuid srefmon: egen N_SingleMom=total(SingleMom)
by ssuid srefmon: egen N_SingleNoCollegeMom = total(SingleNoCollegeMom)
by ssuid srefmon: egen N_ParsWithCollege=total(temp_college)
by ssuid srefmon: egen N_CollegeGradPars=total(MidGenPar_CollegeGrad)
by ssuid srefmon: egen N_ParsEmployed=total(MidGenPar_Employed)
by ssuid srefmon: egen N_ParsPotentialDep=total(MidGenPar_PotentialDep)

gen AnyParsPotentialDeps=(N_ParsPotentialDep>0)
gen AnySingleMom = (N_SingleMom>0)
gen AnySingleNoCollegeMom=(N_SingleNoCollegeMom>0)
gen NoParsWithCollege = (N_ParsWithCollege==0)
gen NoCollegeGradPars = (N_CollegeGradPars==0)
gen AnyParsEmployed = (N_ParsEmployed>0)
drop temp_college

gen RefPerson=(errp==1 | errp==2)
sort ssuid srefmon
by ssuid srefmon: egen N_RefPerson=total(RefPerson)
gen MidGenHasRefPerson=(N_RefPerson>0)

merge m:1 ssuid epppnum using WellBeingVars2008_w10 
keep if _merge==3
drop _merge

gen panel=2008
save OrganizedMidGenParentData2008_w10, replace


** Extract information at household-month level about MAXIMUM amount of EITC eligibility
** for which any mid-gen parent is available.  The goal is to make this info ready 
** for merging with grandparents who live in those same households.

keep panel swave ssuid srefmon r_MaxEITC_thou r_ref_MaxEITC currentyear_r_MaxEITC_thou taxsim_eic_thou xocah AnySingleMom AnySingleNoCollegeMom NoParsWithCollege NoCollegeGradPars AnyParsEmployed MidGenHasRefPerson AnyParsPotentialDeps r_MaxFedEITC_thou ts_eic_fed
sort ssuid srefmon
by ssuid srefmon: egen r_MaxEITC_MidGenMax=max(r_MaxEITC_thou)
by ssuid srefmon: egen r_ref_MaxEITC_MidGenMax=max(r_ref_MaxEITC)
by ssuid srefmon: egen currentyear_r_MaxEITC_MidGenMax=max(currentyear_r_MaxEITC_thou)
by ssuid srefmon: egen taxsim_eic_MidGenMax=max(taxsim_eic_thou)
by ssuid srefmon: egen r_MaxFedEITC_MidGenMax=max(r_MaxFedEITC_thou)
by ssuid srefmon: egen taxsim_eic_Fed_MidGenMax=max(ts_eic_fed)

** Only keep observations of individuals whose EITC eligibility is equal to the max EITC
** eligibility of anyone in the middle generation
keep if r_MaxEITC_MidGenMax == r_MaxEITC_thou

** For later construction of a summary stats table, distinguish between cases of 1 EITC-eligible
** child and 2+ EITC-eligible children of the middle-generation parent who is eligible for the largest
** EITC amount
gen xocah_1or2plus=.
replace xocah_1or2plus = 1 if xocah==1
replace xocah_1or2plus = 2 if xocah>1 

drop xocah r_MaxEITC_thou r_ref_MaxEITC currentyear_r_MaxEITC_thou taxsim_eic_thou ts_eic_fed
duplicates drop

duplicates report ssuid srefmon



save MaxMidGenEITC2008_w10, replace
clear

** Another way to count EITC resources in the middle generation is to sum up amounts received
** by different mid-gen parents.  You don't want to double-count EITC eligibility of a husband
** and wife, but you could assume that all mothers are separate filers and sum up all EITC
** amounts of the different mid-gen mothers. 
use OrganizedMidGenParentData2008_w10

keep panel swave ssuid srefmon r_MaxEITC_thou r_ref_MaxEITC  currentyear_r_MaxEITC_thou currentyear_r_ref_MaxEITC taxsim_eic_thou PotentialMidGenMom r_MaxFedEITC_thou ts_eic_fed
keep if PotentialMidGenMom==1
duplicates report ssuid srefmon
sort ssuid srefmon
by ssuid srefmon: egen Sum_MaxEITC_MidGenMoms = sum(r_MaxEITC_thou)
by ssuid srefmon: egen Sum_ref_MaxEITC_MidGenMoms = sum(r_ref_MaxEITC)
by ssuid srefmon: egen Sum_cy_MaxEITC_MidGenMoms = sum(currentyear_r_MaxEITC_thou)
by ssuid srefmon: egen Sum_cy_ref_MaxEITC_MidGenMoms = sum(currentyear_r_ref_MaxEITC)
by ssuid srefmon: egen Sum_taxsim_eic_MidGenMoms = sum(taxsim_eic_thou)
by ssuid srefmon: egen Sum_MaxFedEITC_MidGenMoms = sum(r_MaxFedEITC_thou)
by ssuid srefmon: egen Sum_taxsim_eic_Fed_MidGenMoms = sum(ts_eic_fed)

keep panel swave ssuid srefmon Sum_MaxEITC_MidGenMoms Sum_ref_MaxEITC_MidGenMoms Sum_cy_MaxEITC_MidGenMoms Sum_cy_ref_MaxEITC_MidGenMoms Sum_taxsim_eic_MidGenMoms Sum_MaxFedEITC_MidGenMoms Sum_taxsim_eic_Fed_MidGenMoms
duplicates drop
save SumMidGenEITC_2008_w10, replace
clear

*/


********************************************************************************
*****************
** 2014, Wave 1
*****************
********************************************************************************

use  MidGenParentData2014_w1
** Merge in count of how many people in household identified this person as his/her mother
merge 1:1 ssuid pnum monthcode using KidCount2014_w1
assert _merge~=1
keep if _merge==3
drop _merge



** Make version of EITC where you DON'T subtract one from year
** before doing EITC calculations
** Can hardcode year for 2014, wave 1.  This wave covers a reference year from Jan
** to Dec of 2013, so EITC amount "in hand" during this time period relies on 2012 rules.
** Using "current year" EITC calculations would use 2013 law
gen EITC_calc_year = 2013
gen xocah = DepCount
quietly do EITC_Calculator.do
drop MaxFedEITC
drop MaxStateEITC
drop StateEITC_NonRefund
rename r_MaxEITC currentyear_r_MaxEITC
rename r_MaxEITC_thou currentyear_r_MaxEITC_thou
rename r_MaxFedEITC_thou currentyear_r_MaxFedEITC_thou
rename r_MaxStateEITC_thou  currentyear_r_MaxStateEITC_thou
rename r_ref_MaxEITC  currentyear_r_ref_MaxEITC
drop real
drop EITC_calc_year xocah


gen EITC_calc_year = 2012
gen xocah = DepCount
quietly do EITC_Calculator.do

gen YearOfTaxCalc = 2012
gen KidsForSimulated = DepCount
replace KidsForSimulated = 3 if KidsForSimulated>3
merge m:1 YearOfTaxCalc tfipsst Married KidsForSimulated using SimulatedEITC
drop if _merge==2
drop _merge
gen taxsim_eic_thou = (ts_eic_fed + ts_eic_state)/1000


rename Married MidGenPar_Married
rename Employed MidGenPar_Employed
rename HSGrad MidGenPar_HSGrad
rename SomeCollege MidGenPar_SomeCollege
rename CollegeGrad MidGenPar_CollegeGrad
rename White MidGenPar_White
rename PotentialDep MidGenPar_PotentialDep


gen SingleMom = (Female==1 & MidGenPar_Married==0)
gen SingleNoCollegeMom = (Female==1 & MidGenPar_Married==0 & MidGenPar_SomeCollege==0 & MidGenPar_CollegeGrad==0)
gen temp_college = (MidGenPar_SomeCollege==1 | MidGenPar_CollegeGrad==1)


sort ssuid monthcode
by ssuid monthcode: egen N_SingleMom=total(SingleMom)
by ssuid monthcode: egen N_SingleNoCollegeMom = total(SingleNoCollegeMom)
by ssuid monthcode: egen N_ParsWithCollege=total(temp_college)
by ssuid monthcode: egen N_CollegeGradPars=total(MidGenPar_CollegeGrad)
by ssuid monthcode: egen N_ParsEmployed=total(MidGenPar_Employed)
by ssuid monthcode: egen N_ParsPotentialDep=total(MidGenPar_PotentialDep)

gen AnyParsPotentialDeps=(N_ParsPotentialDep>0)
gen AnySingleMom = (N_SingleMom>0)
gen AnySingleNoCollegeMom=(N_SingleNoCollegeMom>0)
gen NoParsWithCollege = (N_ParsWithCollege==0)
gen NoCollegeGradPars = (N_CollegeGradPars==0)
gen AnyParsEmployed = (N_ParsEmployed>0)
drop temp_college


gen RefPerson=(erelrp==1 | erelrp==2)
sort ssuid monthcode
by ssuid monthcode: egen N_RefPerson=total(RefPerson)
gen MidGenHasRefPerson=(N_RefPerson>0)



merge m:1 ssuid pnum monthcode using WellBeingVars2014_w1 
keep if _merge==3
drop _merge






*******************************************************************************
** Merge in data on receipt of childcare from a grandparent 

merge 1:1 ssuid pnum monthcode using ChildCareVars_2014_w1
keep if _merge==3
drop _merge 

sort ssuid monthcode
by ssuid monthcode: egen N_GetCCFromGP=total(GetCCFromGP)
gen AnyParsGetCCFromGP = (N_GetCCFromGP>0)



gen panel=2014
save OrganizedMidGenParentData2014_w1, replace



** Extract information at household-month level about MAXIMUM amount of EITC eligibility
** for which any mid-gen parent is available.  The goal is to make this info ready 
** for merging with grandparents who live in those same households.

keep panel swave ssuid monthcode r_MaxEITC_thou r_ref_MaxEITC currentyear_r_MaxEITC_thou taxsim_eic_thou xocah AnySingleMom AnySingleNoCollegeMom NoParsWithCollege NoCollegeGradPars AnyParsEmployed MidGenHasRefPerson AnyParsPotentialDeps r_MaxFedEITC_thou ts_eic_fed AnyParsGetCCFromGP
sort ssuid monthcode
by ssuid monthcode: egen r_MaxEITC_MidGenMax=max(r_MaxEITC_thou)
by ssuid monthcode: egen r_ref_MaxEITC_MidGenMax=max(r_ref_MaxEITC)
by ssuid monthcode: egen currentyear_r_MaxEITC_MidGenMax=max(currentyear_r_MaxEITC_thou)
by ssuid monthcode: egen taxsim_eic_MidGenMax=max(taxsim_eic_thou)
by ssuid monthcode: egen r_MaxFedEITC_MidGenMax=max(r_MaxFedEITC_thou)
by ssuid monthcode: egen taxsim_eic_Fed_MidGenMax=max(ts_eic_fed)



** Only keep observations of individuals whose EITC eligibility is equal to the max EITC
** eligibility of anyone in the middle generation
keep if r_MaxEITC_MidGenMax == r_MaxEITC_thou

** For later construction of a summary stats table, distinguish between cases of 1 EITC-eligible
** child and 2+ EITC-eligible children of the middle-generation parent who is eligible for the largest
** EITC amount
gen xocah_1or2plus=.
replace xocah_1or2plus = 1 if xocah==1
replace xocah_1or2plus = 2 if xocah>1 

drop xocah r_MaxEITC_thou r_ref_MaxEITC currentyear_r_MaxEITC_thou taxsim_eic_thou ts_eic_fed
duplicates drop

duplicates report ssuid monthcode

save MaxMidGenEITC2014_w1, replace
clear

** Another way to count EITC resources in the middle generation is to sum up amounts received
** by different mid-gen parents.  You don't want to double-count EITC eligibility of a husband
** and wife, but you could assume that all mothers are separate filers and sum up all EITC
** amounts of the different mid-gen mothers.  
use OrganizedMidGenParentData2014_w1

keep panel swave ssuid monthcode r_MaxEITC_thou r_ref_MaxEITC  currentyear_r_MaxEITC_thou currentyear_r_ref_MaxEITC taxsim_eic_thou PotentialMidGenMom r_MaxFedEITC_thou ts_eic_fed
keep if PotentialMidGenMom==1
duplicates report ssuid monthcode
sort ssuid monthcode
by ssuid monthcode: egen Sum_MaxEITC_MidGenMoms = sum(r_MaxEITC_thou)
by ssuid monthcode: egen Sum_ref_MaxEITC_MidGenMoms = sum(r_ref_MaxEITC)
by ssuid monthcode: egen Sum_cy_MaxEITC_MidGenMoms = sum(currentyear_r_MaxEITC_thou)
by ssuid monthcode: egen Sum_cy_ref_MaxEITC_MidGenMoms = sum(currentyear_r_ref_MaxEITC)
by ssuid monthcode: egen Sum_taxsim_eic_MidGenMoms = sum(taxsim_eic_thou)
by ssuid monthcode: egen Sum_MaxFedEITC_MidGenMoms = sum(r_MaxFedEITC_thou)
by ssuid monthcode: egen Sum_taxsim_eic_Fed_MidGenMoms = sum(ts_eic_fed)

keep panel swave ssuid monthcode Sum_MaxEITC_MidGenMoms Sum_ref_MaxEITC_MidGenMoms Sum_cy_MaxEITC_MidGenMoms Sum_cy_ref_MaxEITC_MidGenMoms Sum_taxsim_eic_MidGenMoms Sum_MaxFedEITC_MidGenMoms Sum_taxsim_eic_Fed_MidGenMoms
duplicates drop
save SumMidGenEITC_2014_w1, replace


clear




********************************************************************************
*****************
** 2014, Wave 2
*****************
********************************************************************************

use  MidGenParentData2014_w2
** Merge in count of how many people in household identified this person as his/her mother
merge 1:1 ssuid pnum monthcode using KidCount2014_w2
assert _merge~=1
keep if _merge==3
drop _merge



** Make version of EITC where you DON'T subtract one from year
** before doing EITC calculations
** Can hardcode year for 2014, wave 2.  This wave covers a reference year from Jan
** to Dec of 2014, so EITC amount "in hand" during this time period relies on 2013 rules.
** Using "current year" EITC calculations would use 2014 law
gen EITC_calc_year = 2014
gen xocah = DepCount
quietly do EITC_Calculator.do
drop MaxFedEITC
drop MaxStateEITC
drop StateEITC_NonRefund
rename r_MaxEITC currentyear_r_MaxEITC
rename r_MaxEITC_thou currentyear_r_MaxEITC_thou
rename r_MaxFedEITC_thou currentyear_r_MaxFedEITC_thou
rename r_MaxStateEITC_thou  currentyear_r_MaxStateEITC_thou
rename r_ref_MaxEITC  currentyear_r_ref_MaxEITC
drop real
drop EITC_calc_year xocah


gen EITC_calc_year = 2013
gen xocah = DepCount
quietly do EITC_Calculator.do

gen YearOfTaxCalc = 2013
gen KidsForSimulated = DepCount
replace KidsForSimulated = 3 if KidsForSimulated>3
merge m:1 YearOfTaxCalc tfipsst Married KidsForSimulated using SimulatedEITC
drop if _merge==2
drop _merge
gen taxsim_eic_thou = (ts_eic_fed + ts_eic_state)/1000


rename Married MidGenPar_Married
rename Employed MidGenPar_Employed
rename HSGrad MidGenPar_HSGrad
rename SomeCollege MidGenPar_SomeCollege
rename CollegeGrad MidGenPar_CollegeGrad
rename White MidGenPar_White
rename PotentialDep MidGenPar_PotentialDep


gen SingleMom = (Female==1 & MidGenPar_Married==0)
gen SingleNoCollegeMom = (Female==1 & MidGenPar_Married==0 & MidGenPar_SomeCollege==0 & MidGenPar_CollegeGrad==0)
gen temp_college = (MidGenPar_SomeCollege==1 | MidGenPar_CollegeGrad==1)


sort ssuid monthcode
by ssuid monthcode: egen N_SingleMom=total(SingleMom)
by ssuid monthcode: egen N_SingleNoCollegeMom = total(SingleNoCollegeMom)
by ssuid monthcode: egen N_ParsWithCollege=total(temp_college)
by ssuid monthcode: egen N_CollegeGradPars=total(MidGenPar_CollegeGrad)
by ssuid monthcode: egen N_ParsEmployed=total(MidGenPar_Employed)
by ssuid monthcode: egen N_ParsPotentialDep=total(MidGenPar_PotentialDep)

gen AnyParsPotentialDeps=(N_ParsPotentialDep>0)
gen AnySingleMom = (N_SingleMom>0)
gen AnySingleNoCollegeMom=(N_SingleNoCollegeMom>0)
gen NoParsWithCollege = (N_ParsWithCollege==0)
gen NoCollegeGradPars = (N_CollegeGradPars==0)
gen AnyParsEmployed = (N_ParsEmployed>0)
drop temp_college


gen RefPerson=(erelrp==1 | erelrp==2)
sort ssuid monthcode
by ssuid monthcode: egen N_RefPerson=total(RefPerson)
gen MidGenHasRefPerson=(N_RefPerson>0)



merge m:1 ssuid pnum monthcode using WellBeingVars2014_w2 
keep if _merge==3
drop _merge





*******************************************************************************
** Merge in data on receipt of childcare from a grandparent 

merge 1:1 ssuid pnum monthcode using ChildCareVars_2014_w2
keep if _merge==3
drop _merge 

sort ssuid monthcode
by ssuid monthcode: egen N_GetCCFromGP=total(GetCCFromGP)
gen AnyParsGetCCFromGP = (N_GetCCFromGP>0)

gen panel=2014
save OrganizedMidGenParentData2014_w2, replace



** Extract information at household-month level about MAXIMUM amount of EITC eligibility
** for which any mid-gen parent is available.  The goal is to make this info ready 
** for merging with grandparents who live in those same households.

keep panel swave ssuid monthcode r_MaxEITC_thou r_ref_MaxEITC currentyear_r_MaxEITC_thou taxsim_eic_thou xocah AnySingleMom AnySingleNoCollegeMom NoParsWithCollege NoCollegeGradPars AnyParsEmployed MidGenHasRefPerson AnyParsPotentialDeps r_MaxFedEITC_thou ts_eic_fed AnyParsGetCCFromGP
sort ssuid monthcode
by ssuid monthcode: egen r_MaxEITC_MidGenMax=max(r_MaxEITC_thou)
by ssuid monthcode: egen r_ref_MaxEITC_MidGenMax=max(r_ref_MaxEITC)
by ssuid monthcode: egen currentyear_r_MaxEITC_MidGenMax=max(currentyear_r_MaxEITC_thou)
by ssuid monthcode: egen taxsim_eic_MidGenMax=max(taxsim_eic_thou)
by ssuid monthcode: egen r_MaxFedEITC_MidGenMax=max(r_MaxFedEITC_thou)
by ssuid monthcode: egen taxsim_eic_Fed_MidGenMax=max(ts_eic_fed)


** Only keep observations of individuals whose EITC eligibility is equal to the max EITC
** eligibility of anyone in the middle generation
keep if r_MaxEITC_MidGenMax == r_MaxEITC_thou

** For later construction of a summary stats table, distinguish between cases of 1 EITC-eligible
** child and 2+ EITC-eligible children of the middle-generation parent who is eligible for the largest
** EITC amount
gen xocah_1or2plus=.
replace xocah_1or2plus = 1 if xocah==1
replace xocah_1or2plus = 2 if xocah>1 

drop xocah r_MaxEITC_thou r_ref_MaxEITC currentyear_r_MaxEITC_thou taxsim_eic_thou ts_eic_fed
duplicates drop

duplicates report ssuid monthcode

save MaxMidGenEITC2014_w2, replace
clear

** Another way to count EITC resources in the middle generation is to sum up amounts received
** by different mid-gen parents.  You don't want to double-count EITC eligibility of a husband
** and wife, but you could assume that all mothers are separate filers and sum up all EITC
** amounts of the different mid-gen mothers.  
use OrganizedMidGenParentData2014_w2

keep panel swave ssuid monthcode r_MaxEITC_thou r_ref_MaxEITC  currentyear_r_MaxEITC_thou currentyear_r_ref_MaxEITC taxsim_eic_thou PotentialMidGenMom r_MaxFedEITC_thou ts_eic_fed
keep if PotentialMidGenMom==1
duplicates report ssuid monthcode
sort ssuid monthcode
by ssuid monthcode: egen Sum_MaxEITC_MidGenMoms = sum(r_MaxEITC_thou)
by ssuid monthcode: egen Sum_ref_MaxEITC_MidGenMoms = sum(r_ref_MaxEITC)
by ssuid monthcode: egen Sum_cy_MaxEITC_MidGenMoms = sum(currentyear_r_MaxEITC_thou)
by ssuid monthcode: egen Sum_cy_ref_MaxEITC_MidGenMoms = sum(currentyear_r_ref_MaxEITC)
by ssuid monthcode: egen Sum_taxsim_eic_MidGenMoms = sum(taxsim_eic_thou)
by ssuid monthcode: egen Sum_MaxFedEITC_MidGenMoms = sum(r_MaxFedEITC_thou)
by ssuid monthcode: egen Sum_taxsim_eic_Fed_MidGenMoms = sum(ts_eic_fed)

keep panel swave ssuid monthcode Sum_MaxEITC_MidGenMoms Sum_ref_MaxEITC_MidGenMoms Sum_cy_MaxEITC_MidGenMoms Sum_cy_ref_MaxEITC_MidGenMoms Sum_taxsim_eic_MidGenMoms Sum_MaxFedEITC_MidGenMoms  Sum_taxsim_eic_Fed_MidGenMoms
duplicates drop
save SumMidGenEITC_2014_w2, replace


clear






********************************************************************************
*****************
** 2014, Wave 3
*****************
********************************************************************************

use  MidGenParentData2014_w3
** Merge in count of how many people in household identified this person as his/her mother
merge 1:1 ssuid pnum monthcode using KidCount2014_w3
assert _merge~=1
keep if _merge==3
drop _merge



** Make version of EITC where you DON'T subtract one from year
** before doing EITC calculations
** Can hardcode year for 2014, wave 3.  This wave covers a reference year from Jan
** to Dec of 2015, so EITC amount "in hand" during this time period relies on 2014 rules.
** Using "current year" EITC calculations would use 2015 law
gen EITC_calc_year = 2015
gen xocah = DepCount
quietly do EITC_Calculator.do
drop MaxFedEITC
drop MaxStateEITC
drop StateEITC_NonRefund
rename r_MaxEITC currentyear_r_MaxEITC
rename r_MaxEITC_thou currentyear_r_MaxEITC_thou
rename r_MaxFedEITC_thou currentyear_r_MaxFedEITC_thou
rename r_MaxStateEITC_thou  currentyear_r_MaxStateEITC_thou
rename r_ref_MaxEITC  currentyear_r_ref_MaxEITC
drop real
drop EITC_calc_year xocah


gen EITC_calc_year = 2014
gen xocah = DepCount
quietly do EITC_Calculator.do

gen YearOfTaxCalc = 2014
gen KidsForSimulated = DepCount
replace KidsForSimulated = 3 if KidsForSimulated>3
merge m:1 YearOfTaxCalc tfipsst Married KidsForSimulated using SimulatedEITC
drop if _merge==2
drop _merge
gen taxsim_eic_thou = (ts_eic_fed + ts_eic_state)/1000


rename Married MidGenPar_Married
rename Employed MidGenPar_Employed
rename HSGrad MidGenPar_HSGrad
rename SomeCollege MidGenPar_SomeCollege
rename CollegeGrad MidGenPar_CollegeGrad
rename White MidGenPar_White
rename PotentialDep MidGenPar_PotentialDep


gen SingleMom = (Female==1 & MidGenPar_Married==0)
gen SingleNoCollegeMom = (Female==1 & MidGenPar_Married==0 & MidGenPar_SomeCollege==0 & MidGenPar_CollegeGrad==0)
gen temp_college = (MidGenPar_SomeCollege==1 | MidGenPar_CollegeGrad==1)


sort ssuid monthcode
by ssuid monthcode: egen N_SingleMom=total(SingleMom)
by ssuid monthcode: egen N_SingleNoCollegeMom = total(SingleNoCollegeMom)
by ssuid monthcode: egen N_ParsWithCollege=total(temp_college)
by ssuid monthcode: egen N_CollegeGradPars=total(MidGenPar_CollegeGrad)
by ssuid monthcode: egen N_ParsEmployed=total(MidGenPar_Employed)
by ssuid monthcode: egen N_ParsPotentialDep=total(MidGenPar_PotentialDep)

gen AnyParsPotentialDeps=(N_ParsPotentialDep>0)
gen AnySingleMom = (N_SingleMom>0)
gen AnySingleNoCollegeMom=(N_SingleNoCollegeMom>0)
gen NoParsWithCollege = (N_ParsWithCollege==0)
gen NoCollegeGradPars = (N_CollegeGradPars==0)
gen AnyParsEmployed = (N_ParsEmployed>0)
drop temp_college


gen RefPerson=(erelrp==1 | erelrp==2)
sort ssuid monthcode
by ssuid monthcode: egen N_RefPerson=total(RefPerson)
gen MidGenHasRefPerson=(N_RefPerson>0)



merge m:1 ssuid pnum monthcode using WellBeingVars2014_w3 
keep if _merge==3
drop _merge




*******************************************************************************
** Merge in data on receipt of childcare from a grandparent 

merge 1:1 ssuid pnum monthcode using ChildCareVars_2014_w3
keep if _merge==3
drop _merge 

sort ssuid monthcode
by ssuid monthcode: egen N_GetCCFromGP=total(GetCCFromGP)
gen AnyParsGetCCFromGP = (N_GetCCFromGP>0)

gen panel=2014
save OrganizedMidGenParentData2014_w3, replace



** Extract information at household-month level about MAXIMUM amount of EITC eligibility
** for which any mid-gen parent is available.  The goal is to make this info ready 
** for merging with grandparents who live in those same households.

keep panel swave ssuid monthcode r_MaxEITC_thou r_ref_MaxEITC currentyear_r_MaxEITC_thou taxsim_eic_thou xocah AnySingleMom AnySingleNoCollegeMom NoParsWithCollege NoCollegeGradPars AnyParsEmployed MidGenHasRefPerson AnyParsPotentialDeps r_MaxFedEITC_thou ts_eic_fed AnyParsGetCCFromGP
sort ssuid monthcode
by ssuid monthcode: egen r_MaxEITC_MidGenMax=max(r_MaxEITC_thou)
by ssuid monthcode: egen r_ref_MaxEITC_MidGenMax=max(r_ref_MaxEITC)
by ssuid monthcode: egen currentyear_r_MaxEITC_MidGenMax=max(currentyear_r_MaxEITC_thou)
by ssuid monthcode: egen taxsim_eic_MidGenMax=max(taxsim_eic_thou)
by ssuid monthcode: egen r_MaxFedEITC_MidGenMax=max(r_MaxFedEITC_thou)
by ssuid monthcode: egen taxsim_eic_Fed_MidGenMax=max(ts_eic_fed)

** Only keep observations of individuals whose EITC eligibility is equal to the max EITC
** eligibility of anyone in the middle generation
keep if r_MaxEITC_MidGenMax == r_MaxEITC_thou

** For later construction of a summary stats table, distinguish between cases of 1 EITC-eligible
** child and 2+ EITC-eligible children of the middle-generation parent who is eligible for the largest
** EITC amount
gen xocah_1or2plus=.
replace xocah_1or2plus = 1 if xocah==1
replace xocah_1or2plus = 2 if xocah>1 

drop xocah r_MaxEITC_thou r_ref_MaxEITC currentyear_r_MaxEITC_thou taxsim_eic_thou ts_eic_fed
duplicates drop

duplicates report ssuid monthcode

save MaxMidGenEITC2014_w3, replace
clear

** Another way to count EITC resources in the middle generation is to sum up amounts received
** by different mid-gen parents.  You don't want to double-count EITC eligibility of a husband
** and wife, but you could assume that all mothers are separate filers and sum up all EITC
** amounts of the different mid-gen mothers.  
use OrganizedMidGenParentData2014_w3

keep panel swave ssuid monthcode r_MaxEITC_thou r_ref_MaxEITC  currentyear_r_MaxEITC_thou currentyear_r_ref_MaxEITC taxsim_eic_thou PotentialMidGenMom r_MaxFedEITC_thou ts_eic_fed
keep if PotentialMidGenMom==1
duplicates report ssuid monthcode
sort ssuid monthcode
by ssuid monthcode: egen Sum_MaxEITC_MidGenMoms = sum(r_MaxEITC_thou)
by ssuid monthcode: egen Sum_ref_MaxEITC_MidGenMoms = sum(r_ref_MaxEITC)
by ssuid monthcode: egen Sum_cy_MaxEITC_MidGenMoms = sum(currentyear_r_MaxEITC_thou)
by ssuid monthcode: egen Sum_cy_ref_MaxEITC_MidGenMoms = sum(currentyear_r_ref_MaxEITC)
by ssuid monthcode: egen Sum_taxsim_eic_MidGenMoms = sum(taxsim_eic_thou)
by ssuid monthcode: egen Sum_MaxFedEITC_MidGenMoms = sum(r_MaxFedEITC_thou)
by ssuid monthcode: egen Sum_taxsim_eic_Fed_MidGenMoms = sum(ts_eic_fed)

keep panel swave ssuid monthcode Sum_MaxEITC_MidGenMoms Sum_ref_MaxEITC_MidGenMoms Sum_cy_MaxEITC_MidGenMoms Sum_cy_ref_MaxEITC_MidGenMoms Sum_taxsim_eic_MidGenMoms Sum_MaxFedEITC_MidGenMoms Sum_taxsim_eic_Fed_MidGenMoms
duplicates drop
save SumMidGenEITC_2014_w3, replace


clear





********************************************************************************
*****************
** 2014, Wave 4
*****************
********************************************************************************

use  MidGenParentData2014_w4
** Merge in count of how many people in household identified this person as his/her mother
merge 1:1 ssuid pnum monthcode using KidCount2014_w4
assert _merge~=1
keep if _merge==3
drop _merge



** Make version of EITC where you DON'T subtract one from year
** before doing EITC calculations
gen EITC_calc_year = 2016
gen xocah = DepCount
quietly do EITC_Calculator.do
drop MaxFedEITC
drop MaxStateEITC
drop StateEITC_NonRefund
rename r_MaxEITC currentyear_r_MaxEITC
rename r_MaxEITC_thou currentyear_r_MaxEITC_thou
rename r_MaxFedEITC_thou currentyear_r_MaxFedEITC_thou
rename r_MaxStateEITC_thou  currentyear_r_MaxStateEITC_thou
rename r_ref_MaxEITC  currentyear_r_ref_MaxEITC
drop real
drop EITC_calc_year xocah


gen EITC_calc_year = 2015
gen xocah = DepCount
quietly do EITC_Calculator.do

gen YearOfTaxCalc = 2015
gen KidsForSimulated = DepCount
replace KidsForSimulated = 3 if KidsForSimulated>3
merge m:1 YearOfTaxCalc tfipsst Married KidsForSimulated using SimulatedEITC
drop if _merge==2
drop _merge
gen taxsim_eic_thou = (ts_eic_fed + ts_eic_state)/1000


rename Married MidGenPar_Married
rename Employed MidGenPar_Employed
rename HSGrad MidGenPar_HSGrad
rename SomeCollege MidGenPar_SomeCollege
rename CollegeGrad MidGenPar_CollegeGrad
rename White MidGenPar_White
rename PotentialDep MidGenPar_PotentialDep


gen SingleMom = (Female==1 & MidGenPar_Married==0)
gen SingleNoCollegeMom = (Female==1 & MidGenPar_Married==0 & MidGenPar_SomeCollege==0 & MidGenPar_CollegeGrad==0)
gen temp_college = (MidGenPar_SomeCollege==1 | MidGenPar_CollegeGrad==1)


sort ssuid monthcode
by ssuid monthcode: egen N_SingleMom=total(SingleMom)
by ssuid monthcode: egen N_SingleNoCollegeMom = total(SingleNoCollegeMom)
by ssuid monthcode: egen N_ParsWithCollege=total(temp_college)
by ssuid monthcode: egen N_CollegeGradPars=total(MidGenPar_CollegeGrad)
by ssuid monthcode: egen N_ParsEmployed=total(MidGenPar_Employed)
by ssuid monthcode: egen N_ParsPotentialDep=total(MidGenPar_PotentialDep)

gen AnyParsPotentialDeps=(N_ParsPotentialDep>0)
gen AnySingleMom = (N_SingleMom>0)
gen AnySingleNoCollegeMom=(N_SingleNoCollegeMom>0)
gen NoParsWithCollege = (N_ParsWithCollege==0)
gen NoCollegeGradPars = (N_CollegeGradPars==0)
gen AnyParsEmployed = (N_ParsEmployed>0)
drop temp_college


gen RefPerson=(erelrp==1 | erelrp==2)
sort ssuid monthcode
by ssuid monthcode: egen N_RefPerson=total(RefPerson)
gen MidGenHasRefPerson=(N_RefPerson>0)



merge m:1 ssuid pnum monthcode using WellBeingVars2014_w4 
keep if _merge==3
drop _merge





*******************************************************************************
** Merge in data on receipt of childcare from a grandparent 

merge 1:1 ssuid pnum monthcode using ChildCareVars_2014_w4
keep if _merge==3
drop _merge 

sort ssuid monthcode
by ssuid monthcode: egen N_GetCCFromGP=total(GetCCFromGP)
gen AnyParsGetCCFromGP = (N_GetCCFromGP>0)

gen panel=2014
save OrganizedMidGenParentData2014_w4, replace



** Extract information at household-month level about MAXIMUM amount of EITC eligibility
** for which any mid-gen parent is available.  The goal is to make this info ready 
** for merging with grandparents who live in those same households.

keep panel swave ssuid monthcode r_MaxEITC_thou r_ref_MaxEITC currentyear_r_MaxEITC_thou taxsim_eic_thou xocah AnySingleMom AnySingleNoCollegeMom NoParsWithCollege NoCollegeGradPars AnyParsEmployed MidGenHasRefPerson AnyParsPotentialDeps r_MaxFedEITC_thou ts_eic_fed AnyParsGetCCFromGP
sort ssuid monthcode
by ssuid monthcode: egen r_MaxEITC_MidGenMax=max(r_MaxEITC_thou)
by ssuid monthcode: egen r_ref_MaxEITC_MidGenMax=max(r_ref_MaxEITC)
by ssuid monthcode: egen currentyear_r_MaxEITC_MidGenMax=max(currentyear_r_MaxEITC_thou)
by ssuid monthcode: egen taxsim_eic_MidGenMax=max(taxsim_eic_thou)
by ssuid monthcode: egen r_MaxFedEITC_MidGenMax=max(r_MaxFedEITC_thou)
by ssuid monthcode: egen taxsim_eic_Fed_MidGenMax=max(ts_eic_fed)

** Only keep observations of individuals whose EITC eligibility is equal to the max EITC
** eligibility of anyone in the middle generation
keep if r_MaxEITC_MidGenMax == r_MaxEITC_thou

** For later construction of a summary stats table, distinguish between cases of 1 EITC-eligible
** child and 2+ EITC-eligible children of the middle-generation parent who is eligible for the largest
** EITC amount
gen xocah_1or2plus=.
replace xocah_1or2plus = 1 if xocah==1
replace xocah_1or2plus = 2 if xocah>1 

drop xocah r_MaxEITC_thou r_ref_MaxEITC currentyear_r_MaxEITC_thou taxsim_eic_thou ts_eic_fed
duplicates drop

duplicates report ssuid monthcode

save MaxMidGenEITC2014_w4, replace
clear

** Another way to count EITC resources in the middle generation is to sum up amounts received
** by different mid-gen parents.  You don't want to double-count EITC eligibility of a husband
** and wife, but you could assume that all mothers are separate filers and sum up all EITC
** amounts of the different mid-gen mothers. 
use OrganizedMidGenParentData2014_w4

keep panel swave ssuid monthcode r_MaxEITC_thou r_ref_MaxEITC  currentyear_r_MaxEITC_thou currentyear_r_ref_MaxEITC taxsim_eic_thou PotentialMidGenMom r_MaxFedEITC_thou ts_eic_fed
keep if PotentialMidGenMom==1
duplicates report ssuid monthcode
sort ssuid monthcode
by ssuid monthcode: egen Sum_MaxEITC_MidGenMoms = sum(r_MaxEITC_thou)
by ssuid monthcode: egen Sum_ref_MaxEITC_MidGenMoms = sum(r_ref_MaxEITC)
by ssuid monthcode: egen Sum_cy_MaxEITC_MidGenMoms = sum(currentyear_r_MaxEITC_thou)
by ssuid monthcode: egen Sum_cy_ref_MaxEITC_MidGenMoms = sum(currentyear_r_ref_MaxEITC)
by ssuid monthcode: egen Sum_taxsim_eic_MidGenMoms = sum(taxsim_eic_thou)
by ssuid monthcode: egen Sum_MaxFedEITC_MidGenMoms = sum(r_MaxFedEITC_thou)
by ssuid monthcode: egen Sum_taxsim_eic_Fed_MidGenMoms = sum(ts_eic_fed)

keep panel swave ssuid monthcode Sum_MaxEITC_MidGenMoms Sum_ref_MaxEITC_MidGenMoms Sum_cy_MaxEITC_MidGenMoms Sum_cy_ref_MaxEITC_MidGenMoms Sum_taxsim_eic_MidGenMoms Sum_MaxFedEITC_MidGenMoms Sum_taxsim_eic_Fed_MidGenMoms
duplicates drop
save SumMidGenEITC_2014_w4, replace


clear



*******************************************************

use OrganizedMidGenParentData1990_w3
append using OrganizedMidGenParentData1990_w4

append using OrganizedMidGenParentData1991_w3
append using OrganizedMidGenParentData1991_w7

append using OrganizedMidGenParentData1992_w4
append using OrganizedMidGenParentData1992_w6

append using OrganizedMidGenParentData1993_w3
append using OrganizedMidGenParentData1993_w6
append using OrganizedMidGenParentData1993_w7


append using OrganizedMidGenParentData1996_w3
append using OrganizedMidGenParentData1996_w6
append using OrganizedMidGenParentData1996_w9
append using OrganizedMidGenParentData1996_w12

append using OrganizedMidGenParentData2001_w3
append using OrganizedMidGenParentData2001_w6
append using OrganizedMidGenParentData2001_w9

append using OrganizedMidGenParentData2004_w3
append using OrganizedMidGenParentData2004_w6

append using OrganizedMidGenParentData2008_w4
append using OrganizedMidGenParentData2008_w7
append using OrganizedMidGenParentData2008_w10

append using OrganizedMidGenParentData2014_w1
append using OrganizedMidGenParentData2014_w2
append using OrganizedMidGenParentData2014_w3
append using OrganizedMidGenParentData2014_w4



tab panel, missing

** Merge in a measure of EITC at the household level, the sum over all of
** the mid-gen mothers in the household.
merge m:1 panel swave suid addid srefmon using SumMidGenEITC_1990_w3
drop _merge
merge m:1 panel swave suid addid srefmon using SumMidGenEITC_1990_w4, update
drop _merge
merge m:1 panel swave suid addid srefmon using SumMidGenEITC_1991_w3, update
drop _merge
merge m:1 panel swave suid addid srefmon using SumMidGenEITC_1991_w7, update
drop _merge
merge m:1 panel swave suid addid srefmon using SumMidGenEITC_1992_w4, update
drop _merge
merge m:1 panel swave suid addid srefmon using SumMidGenEITC_1992_w6, update
drop _merge
merge m:1 panel swave suid addid srefmon using SumMidGenEITC_1993_w3, update
drop _merge
merge m:1 panel swave suid addid srefmon using SumMidGenEITC_1993_w6, update
drop _merge
merge m:1 panel swave ssuid srefmon using SumMidGenEITC_1996_w3, update
drop _merge
merge m:1 panel swave ssuid srefmon using SumMidGenEITC_1996_w6, update
drop _merge
merge m:1 panel swave ssuid srefmon using SumMidGenEITC_1996_w9, update
drop _merge
merge m:1 panel swave ssuid srefmon using SumMidGenEITC_1996_w12, update
drop _merge
merge m:1 panel swave ssuid srefmon using SumMidGenEITC_2001_w3, update
drop _merge
merge m:1 panel swave ssuid srefmon using SumMidGenEITC_2001_w6, update
drop _merge
merge m:1 panel swave ssuid srefmon using SumMidGenEITC_2001_w9, update
drop _merge
merge m:1 panel swave ssuid srefmon using SumMidGenEITC_2004_w3, update
drop _merge
merge m:1 panel swave ssuid srefmon using SumMidGenEITC_2004_w6, update
drop _merge
merge m:1 panel swave ssuid srefmon using SumMidGenEITC_2008_w4, update
drop _merge
merge m:1 panel swave ssuid srefmon using SumMidGenEITC_2008_w7, update
drop _merge
merge m:1 panel swave ssuid srefmon using SumMidGenEITC_2008_w10, update
drop _merge
merge m:1 panel ssuid monthcode using SumMidGenEITC_2014_w1, update
drop _merge
merge m:1 panel ssuid monthcode using SumMidGenEITC_2014_w2, update
drop _merge
merge m:1 panel ssuid monthcode using SumMidGenEITC_2014_w3, update
drop _merge
merge m:1 panel ssuid monthcode using SumMidGenEITC_2014_w4, update
drop _merge


** Should drop cases in which tfipsst code does not uniquely identify state, when the 
** group of states includes some with different EITC parameters
drop if (panel==1990| panel==1992 | panel==1993) & (tfipsst==61 | tfipsst==62 | tfipsst==63)
drop if panel==1996 & (tfipsst==61 | tfipsst==62)
drop if panel==2001 & (tfipsst==61 | tfipsst==62)
** For 2014 and later years, drop any cases with state identifier indicating Puerto Rico or foreign country
drop if (panel==2014 | panel==2018)  & (tehc_st=="60" | tehc_st=="61")


tab panel, missing


** Merge in the other measure of EITC at the household level, the max of any mid-gen parent
merge m:1 panel swave suid addid srefmon using MaxMidGenEITC1990_w3
drop _merge
merge m:1 panel swave suid addid srefmon using MaxMidGenEITC1990_w4, update
drop _merge
merge m:1 panel swave suid addid srefmon using MaxMidGenEITC1991_w3, update
drop _merge
merge m:1 panel swave suid addid srefmon using MaxMidGenEITC1991_w7, update
drop _merge
merge m:1 panel swave suid addid srefmon using MaxMidGenEITC1992_w4, update
drop _merge
merge m:1 panel swave suid addid srefmon using MaxMidGenEITC1992_w6, update
drop _merge
merge m:1 panel swave suid addid srefmon using MaxMidGenEITC1993_w3, update
drop _merge
merge m:1 panel swave suid addid srefmon using MaxMidGenEITC1993_w6, update
drop _merge
merge m:1 panel swave ssuid srefmon using MaxMidGenEITC1996_w3, update
drop _merge
merge m:1 panel swave ssuid srefmon using MaxMidGenEITC1996_w6, update
drop _merge
merge m:1 panel swave ssuid srefmon using MaxMidGenEITC1996_w9, update
drop _merge
merge m:1 panel swave ssuid srefmon using MaxMidGenEITC1996_w12, update
drop _merge
merge m:1 panel swave ssuid srefmon using MaxMidGenEITC2001_w3, update
drop _merge
merge m:1 panel swave ssuid srefmon using MaxMidGenEITC2001_w6, update
drop _merge
merge m:1 panel swave ssuid srefmon using MaxMidGenEITC2001_w9, update
drop _merge
merge m:1 panel swave ssuid srefmon using MaxMidGenEITC2004_w3, update
drop _merge
merge m:1 panel swave ssuid srefmon using MaxMidGenEITC2004_w6, update
drop _merge
merge m:1 panel swave ssuid srefmon using MaxMidGenEITC2008_w4, update
drop _merge
merge m:1 panel swave ssuid srefmon using MaxMidGenEITC2008_w7, update
drop _merge
merge m:1 panel swave ssuid srefmon using MaxMidGenEITC2008_w10, update
drop _merge
merge m:1 panel ssuid monthcode using MaxMidGenEITC2014_w1, update
drop _merge
merge m:1 panel ssuid monthcode using MaxMidGenEITC2014_w2, update
drop _merge
merge m:1 panel ssuid monthcode using MaxMidGenEITC2014_w3, update
drop _merge
merge m:1 panel ssuid monthcode using MaxMidGenEITC2014_w4, update
drop _merge



** Person identifer

egen UniquePersonID_p2014andlater = group(panel ssuid pnum)
replace UniquePersonID_p2014andlater = . if (panel==1990 | panel==1991 | panel==1992 | panel==1993 | panel==1996 | panel==2001 | panel==2004 | panel==2008)

egen UniquePersonID_midyears = group(panel ssuid epppnum )
replace UniquePersonID_midyears = . if (panel==1990 | panel==1991 | panel==1992 | panel==1993 | panel==2014 | panel==2018)

egen UniquePersonID_p1993 = group(panel suid entry pnum)
replace UniquePersonID_p1993 = . if (panel==1996 | panel==2001 | panel==2004 | panel==2008 | panel==2014)

gen temp_UniquePersonID = UniquePersonID_midyears
replace temp_UniquePersonID = UniquePersonID_p1993 if (panel==1990 | panel==1991| panel==1992 | panel==1993) 
replace temp_UniquePersonID = UniquePersonID_p2014andlater if (panel==2014 | panel==2018)

egen UniquePersonID = group(panel temp_UniquePersonID)


** Household identifier

egen later_UniqueHouseholdID = group(panel ssuid )
replace later_UniqueHouseholdID = . if (panel==1990 | panel==1991 | panel==1992 | panel==1993)

egen earlier_UniqueHouseholdID = group(panel suid entry)
replace earlier_UniqueHouseholdID = . if (panel==1996 | panel==2001 | panel==2004 | panel==2008 | panel==2014 | panel==2018)

gen temp_UniqueHouseholdID = later_UniqueHouseholdID
replace temp_UniqueHouseholdID = earlier_UniqueHouseholdID if (panel==1990 | panel==1991 | panel==1992| panel==1993)

egen UniqueHouseholdID = group(panel temp_UniqueHouseholdID)


gen ExcellentVeryGoodHealth = (ehltstat==1 | ehltstat==2)


gen AgeSq = tage*tage


gen r_DebtAmount = DebtAmount*real
gen ln_r_Debt = ln(1+r_DebtAmount)



gen r_AssetAmount = AssetAmount*real
gen r_CheckingAmount = CheckingAmount*real
gen r_SavingsCheckingA = SavingsCheckingAmount*real

gen r_taxsim_eic_thou= taxsim_eic_thou*real

gen ln_r_AssetAmount = ln(1+r_AssetAmount)
gen ln_r_CheckingAmount=ln(1+r_CheckingAmount)
gen ln_r_SavingsChecking = ln(1+r_SavingsCheckingA)




** Month-by-month variation is not meaningful for the well-being variables that are asked about longer reference period
** Figure out appropriate way to trim down

duplicates report UniquePersonID swave panel
gen uniform_srefmon = srefmon
replace uniform_srefmon = monthcode if (panel==2014 | panel==2018)
sort panel swave UniquePersonID 
by panel swave UniquePersonID: egen min_srefmon = min(uniform_srefmon)
keep if uniform_srefmon==min_srefmon
drop min_srefmon
duplicates report UniquePersonID panel



** For some analysis, it will be useful to identify earliest observation of a given individual
sort UniquePersonID swave
by UniquePersonID: egen min_swave=min(swave)


** Add in info about state-level unemployment, top tax rate, gdp, etc
recode rhcalyr 90=1990
recode rhcalyr 91=1991
recode rhcalyr 92=1992
recode rhcalyr 93=1993
recode rhcalyr 94=1994
recode rhcalyr 95=1995
replace rhcalyr = 2013 if panel==2014 & swave==1
replace rhcalyr = 2014 if panel==2014 & swave==2
replace rhcalyr = 2015 if panel==2014 & swave==3
replace rhcalyr = 2016 if panel==2014 & swave==4
replace rhcalyr = 2017 if panel==2018

merge m:1 tfipsst rhcalyr using OrganizedStateControls


tab _merge
keep if _merge==3
drop _merge

gen r_MaxWelf = MaxWelf*real



** Create variables that will be helpful in identifying samples for health-related
** regressions and samples for debt-related regressions
gen MissingHealthInfo=(ehltstat==. | ehltstat==0)
gen MissingDebtInfo = (AnyDebt==. | AnyCCDebt==. | ln_r_Debt==. | AnyChecking==. | ln_r_CheckingAmount==.)



** Create a dummy equal to one for those who have all the info needed to appear in health regressions
gen InHealthReg =(ehltst~=. & r_MaxEITC_thou~=. & tage~=. & MidGenPar_Married~=. & MidGenPar_White~=. &  unemp_rate~=. & r_MaxWelf~=. & state_rate_wages~=. & minwage~=. & gdp~=. & any_w_a_extended~=. & rhcalyr~=. & tfipsst~=. & MissingHealthInfo==0)



** Create a dummy equal to one for those who have all the info needed to appear in debt regressions
gen InDebtReg =(AnyDebt~=. & r_MaxEITC_thou~=. & tage~=. & MidGenPar_Married~=. & MidGenPar_White~=. &  unemp_rate~=. & r_MaxWelf~=. & state_rate_wages~=. & minwage~=. & gdp~=. & any_w_a_extended~=. & rhcalyr~=. & tfipsst~=. & MissingDebtInfo==0)


** It may be useful to identify earliest observation with non-missing health info (and all the other
** info needed to be included in health regression)
gen swave_temp = swave
replace swave_temp = . if MissingHealthInfo==1 | InHealthReg~=1
sort UniquePersonID
by UniquePersonID: egen min_swave_Health = min(swave_temp)
drop swave_temp


** It may be useful to identify earliest observation with non-missing financial info (and all the other
** info needed to be included in financial regressions)
gen swave_temp = swave
replace swave_temp = . if MissingDebtInfo==1 | InDebtReg~=1
sort UniquePersonID
by UniquePersonID: egen min_swave_Debt = min(swave_temp)
drop swave_temp


gen Annualized_HH_inc = thtotinc*12
gen r_Annualized_HH_inc = real*Annualized_HH_inc


drop if tfipsst==.

save OrganizedParentLevelData, replace


